【发布时间】:2015-10-20 13:58:32
【问题描述】:
doesNotCompile 方法仅接受仅包含 Label[A] 条目的 HList。有一个 Mapper 可以将 Label[A] 转换为 String(准确地说:Const[String]#λ)。
但是,当我应用映射器时,返回类型是 ev1.Out。我知道这实际上是一个只有字符串的 HList,但是我怎样才能说服编译器呢?
import shapeless._
import shapeless.poly._
import shapeless.ops.hlist._
import shapeless.UnaryTCConstraint._
object Util {
case class Label[A](name: String, value: A)
object GetLabelName extends (Label ~> Const[String]#λ) {
def apply[A](label: Label[A]) = label.name
}
}
object Main {
import Util._
def bar(l: List[String]) = ???
def compiles = {
val names = "a" :: "b" :: HNil
bar(names.toList)
}
// A is an HList whose members are all Label[_]
def doesNotCompile[A <: HList : *->*[Label]#λ](labels: A)(
implicit ev1: Mapper[GetLabelName.type, A]) = {
// implicit ev1: Mapper[String, A]) = {
val names = labels map GetLabelName
// names is of type `ev1.Out` - I want it to be an HList of Strings
bar(names.toList)
// error: could not find implicit value for parameter toTraversableAux:
// shapeless.ops.hlist.ToTraversable.Aux[ev1.Out,List,Lub]
}
}
这是完整的要点,包括。 build.sbt - 下载并运行 sbt compile: https://gist.github.com/mpollmeier/6c53e375d88a32016250
【问题讨论】: