[英]Summoning an HList of type classes
Given an arbitrary case class Cc( a: String, b: Int )
and a type class trait Tc[T]
, is there a way to materialize a simple HList
(or preferably a labelled record) that contains a type class instance for each parameter of Cc
? 给定任意case class Cc( a: String, b: Int )
和类型类trait Tc[T]
,是否有一种方法可以实现包含每个参数的类型类实例的简单HList
(或最好是标记记录) Cc
?
def resolveTypeClasses[C] = ???
val resolved: Tc[String] :: Tc[Int] :: HNil = resolveTypeClasses[Cc]
It's hard to make single type parameter function because of need to resolve intermediate Generic
. 由于需要解析中间Generic
很难制作单一类型参数函数。
So from my noob point of view it should be two parameter def
, or sequence of two def
s, each taking single type parameter. 所以从我的noob观点来看,它应该是两个参数def
,或两个def
的序列,每个参数都采用单一类型参数。 Second variant could be implemented like: 第二种变体可以实现如下:
import shapeless._
trait Tc[T]
case class Cc(a: String, b: Int)
trait ResolveTC[L] {
type Out <: HList
val out: Out
}
implicit object ResolveHNilTC extends ResolveTC[HNil] {
type Out = HNil
val out: HNil = HNil
}
implicit def resolveHListTC[X, XS <: HList](implicit tc: Tc[X], rest: ResolveTC[XS]) =
new ResolveTC[X :: XS] {
type Out = Tc[X] :: rest.Out
val out = tc :: rest.out
}
class TCResolver[C] {
def get[L <: HList](implicit gen: Generic.Aux[C, L], resolve: ResolveTC[L]): resolve.Out = resolve.out
}
def resolveTypeClasses[C] = new TCResolver[C]
implicit case object StringInst extends Tc[String]
implicit case object IntInst extends Tc[Int]
with that implementation 有了这个实现
val resolved = resolveTypeClasses[Cc].get
will produce the 将产生
StringInst :: IntInst :: HNil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.