简体   繁体   English

shapeless-从基础HList派生类型构造函数HList的选择器

[英]shapeless - derive Selector of type constructor HList from base HList

I have a Country enum and an HList that is a subset of enum values: 我有一个Country枚举和一个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

I have a Price class and PriceTable as below: 我有一个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)

The priceFor statement does not compile since a Selector[PriceList, Price[C]] is not in scope. 由于Selector[PriceList, Price[C]]不在范围内Selector[PriceList, Price[C]]无法编译priceFor语句。

The code calling priceFor only has access to a Selector[CountryList, C] but not a Selector[PriceList, Price[C]] given that CountryList =:= countries.type . 给定CountryList =:= countries.type Selector[PriceList, Price[C]]调用priceFor的代码只能访问Selector[CountryList, C]但不能访问Selector[PriceList, Price[C]]

Is there a way to derive a Selector[PriceList, Price[C]] from a Selector[CountryList, C] given that Comapped.Aux[PriceList, Price, CountryList] proves the relationship? 给定Comapped.Aux[PriceList, Price, CountryList]证明了这种关系Comapped.Aux[PriceList, Price, CountryList]是否有一种方法可以从Selector[CountryList, C]派生Selector[PriceList, Price[C]]

If what you want to achieve is to get the price of a given country type, since Country is a type parameter of Price , how about: 如果要实现的是获取给定国家类型的价格,由于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]]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM