shapeless - 从基础HList派生类型构造函数HList的Selector

时间:2017-06-12 22:24:13

标签: scala shapeless

我有一个True枚举和一个枚举值为子集的HList:

Country

我有一个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] Selector[PriceList, Price[C]]

如果CountryList =:= countries.type证明了这种关系,有没有办法从Selector[PriceList, Price[C]]推导Selector[CountryList, C]

1 个答案:

答案 0 :(得分:0)

如果您要实现的目标是获取给定国家/地区类型的价格,那么Country是[{1}}的类型参数,那么如何:

Price