有人可以解释一下为什么这段代码可以编译并像魅力一样工作吗:
val a = true :: Some(5) :: true :: HNil
a.select[Some[Int]] // Some(5)
但是这个失败了:
def foo[HL <: HList](a: HL): Some[Int] = {
a.select[Some[Int]] // fails
}
Implicit not found: shapeless.Ops.Selector[tp, Some[Int]]. You requested an element of type Some[Int], but there is none in the HList tp.
a.select[Some[Int]]
我不想传递 Selector 的实例。
我也尝试过:
type tp = Boolean :: Option[Int] :: Boolean :: HNil
def foo(a: tp): Some[Int] = a.select[Some[Int]] // fails
foo(a)
但失败并显示一条消息:
Implicit not found: shapeless.Ops.Selector[tp, Some[Int]]. You requested an element of type Some[Int], but there is none in the HList tp.
a.select[Some[Int]]
def foo[HL <: HList](a: HL)
(implicit sel: ops.hlist.Selector[HL, Some[Int]]): Some[Int] = {
a.select[Some[Int]]
}