2017-06-12 75 views
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]证明的关系?

回答

0

如果你想达到什么是让某个国家型的价格,因为CountryPrice类型参数,怎么样:

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]] 
} 
+0

感谢您的快速回复。 调用'priceFor'的代码在范围中没有'Selector [PriceList,Price [C]]',但只有'Selector [CountryList,C]'。因此需要从另一个派生出一个。 –