2
我有一个Country
枚举和HList是枚举值的子集:不成形 - 从基HList派生类型构造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)
由于Selector[PriceList, Price[C]]
不在范围之内,所以priceFor
语句不编译。
调用priceFor
的代码只能访问一个Selector[CountryList, C]
但不是Selector[PriceList, Price[C]]
因为CountryList =:= countries.type
。
有没有一种方法来推导从Selector[CountryList, C]
一个Selector[PriceList, Price[C]]
因为Comapped.Aux[PriceList, Price, CountryList]
证明的关系?
感谢您的快速回复。 调用'priceFor'的代码在范围中没有'Selector [PriceList,Price [C]]',但只有'Selector [CountryList,C]'。因此需要从另一个派生出一个。 –