我无法理解Shapeless记录选择器与scala类型推断交互的方式.我试图创建一个可以通过按键将未成形的纪录抢实地,只有当字段的值具有一定的一元型构造,在这种特殊情况下的方法Vector[_],然后抢推断类型的内在价值V出来的的Vector,在这种情况下有Vector.apply().
我觉得我很亲密.这是有效的,具体内部类型Int:
val record = ( "a" ->> Vector(0,2,4) ) :: ( "b" ->> Set(1,3,5) ) :: HNil
def getIntFromVectorField[L <: HList](l: L, fieldName:Witness, index:Int)(implicit
sel: Selector.Aux[L, fieldName.T, Vector[Int]]
):Int = l(fieldName).apply(index)
getIntFromVectorField(record,"a",1) // Returns 1
getIntFromVectorField(record,"b",0) // Does not compile, as intended
Run Code Online (Sandbox Code Playgroud)
但如果我试图推断内部类型,它会失败:
def getValueFromVectorField[L <: HList,V](l:L, fieldName:Witness, index:Int)(implicit
sel: Selector.Aux[L,fieldName.T,Vector[V]]
):V = l(fieldName).apply(index) // Compiles
getValueFromVectorField(record,"a",1) // Why does this not compile?
Run Code Online (Sandbox Code Playgroud)
这是完整的错误:
could not find implicit value …Run Code Online (Sandbox Code Playgroud)