我有一个Country
枚举和一个HList,它是枚举值的子集:
import shapeless._
import iops.hlist.{Comapped, Selector}
sealed trait Country
case object US extends Country
case object DE extends Country
case object CA extends Country
...
val countries = US :: DE :: HNil
我有一个Price
课,PriceTable
如下:
case class Price[C <: Country](value: Double)
class PricesTable[CountryList <: HList, PriceList <: HList](prices: PriceList)
(implicit comapped: Comapped.Aux[PriceList, Price, CountryList]) {
def priceFor[C <: Country](implicit selector: Selector[CountryList, C]: Price[C] =
prices.select[Price[C]]
}
val pricesTable = new PricesTable(Price[US.type](20) :: Price[DE.type](25) :: HNil)
priceFor
语句不会编译,因为Selector[PriceList, Price[C]]
不在范围内。
调用priceFor
的代码只能访问Selector[CountryList, C]
,而不能访问给定CountryList =:= countries.type
Selector[PriceList, Price[C]]
。
有没有办法从Selector[CountryList, C]
中得出Selector[PriceList, Price[C]]
,因为Comapped.Aux[PriceList, Price, CountryList]
证明了这种关系?
如果要实现的是获取给定国家/地区类型的价格,因为Country
是 Price
的类型参数,怎么样:
class PricesTable[PriceList <: HList](prices: PriceList)(implicit lubC: LUBConstraint[PriceList, Price[_]]) {
def priceFor[C <: Country](implicit selector: Selector[PriceList, Price[C]]): Price[C] =
prices.select[Price[C]]
}