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