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]'。因此需要從另一個派生出一個。 –