【发布时间】:2015-03-13 01:18:23
【问题描述】:
我正在使用 shapeless 2.1.0 -scala 2.11,jdk 1.7:我有一个 trait
trait Input[T]{
def location:String
}
object location extends Poly1 {
implicit def caseInput[T] = at[Input[T]](l => l.location)
}
val list = new Input[String] {def location:String="/tmp"} :: HNil
list.map(location)
这会在我的控制台中正确返回
shapeless2.::[String,shapeless2.HNil] = /tmp :: HNil
但是,当我在函数中具有完全相同的逻辑时 - HList 从另一个函数调用返回给我,并且我在其上映射函数,我得到一个编译时错误
:could not find implicit value for parameter mapper: shapeless.ops.hlist.Mapper[location.type,shapeless.::[Input[String]{...},shapeless.HNil]]
我怀疑我可能遗漏了一些暗示。我检查了无形的测试和文档——希望我没有错过任何太明显的东西。
如果问题不是很明显,我可以创建一个完整的示例来重新创建问题 - 感谢阅读。
最好, 阿米特
更新:举个例子
特征输入[T]{ 默认位置:字符串 定义值:T }
trait location extends Poly1 {
implicit def caseList[T] = at[Input[T]](l => l.location)
}
object testfun extends location {
implicit val atString = at[Input[String]](_.location)
implicit val atInt = at[Input[Int]](_.location)
implicit val atLong = at[Input[Long]](_.location)
}
def inputs:HList={
val str = new Input[String]{
override def location: String = "/tmp/string"
override def value: String = "here it is"
}
val ints = new Input[Int]{
override def location: String = "/tmp/1"
override def value: Int = 1
}
val longs = new Input[Long]{
override def location: String = "/tmp/1l"
override def value: Long = 1l
}
str::ints::longs::HNil
}
>>>println(inputs.map(testfun))
could not find implicit value for parameter mapper: shapeless.ops.hlist.Mapper[HListTest.testfun.type,shapeless.HList]
如果我要删除 def 输入的返回类型,我不会收到任何错误。
【问题讨论】:
-
如果不能在失败的情况下看到调用上下文,真的无法提供帮助。
-
是的。抱歉迈尔斯 - 附上一个例子
-
您已将
inputs的结果类型定义为HList,这会丢弃Mapper所需的类型信息。尝试将其定义为Input[String] :: Input[Int] :: Input[Long] :: HNil或允许其被推断。 -
如何推断?我上面的例子是简化的——在我的真实代码中,抽象类是类型化的,并提供类似于的输入返回类型