HList Ops - 如何构造类型类?

问题描述 投票:0回答:1

有人可以解释一下为什么这段代码可以编译并像魅力一样工作吗:

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]]
scala implicit shapeless hlist
1个回答
0
投票

这个怎么样

def foo[HL <: HList](a: HL)
                    (implicit sel: ops.hlist.Selector[HL, Some[Int]]): Some[Int] = {
  a.select[Some[Int]]                       
}
© www.soinside.com 2019 - 2024. All rights reserved.