def unreturn(lx: Sized[List[Int], Nat._1]): Int = lx head
Run Code Online (Sandbox Code Playgroud)
如何使用shapeless获取一个元素列表的头部?这不编译:
No implicit view available from List[Int] =>
scala.collection.GenTraversableLike[v.A,List[Int]].
not enough arguments for method sizedOps:
(implicit evidence$2: List[Int] =>
scala.collection.GenTraversableLike[v.A,List[Int]])shapeless.
SizedOps[v.A,List[Int],shapeless.Nat._1]. Unspecified value parameter evidence$2.
Run Code Online (Sandbox Code Playgroud)
您只需要在参数类型中包含类型成员:
def unreturn(lx: Sized[List[Int], Nat._1] { type A = Int }): Int = lx.head
Run Code Online (Sandbox Code Playgroud)
问题是你知道没有(安全)方法Sized[List[Int], _1]与其他任何方法一起构建A,但编译器仍然需要证据.