这取决于您的用例。
HList
对于类型级代码是有用的,因此您不仅应该传递给您的方法
HList公司
,还有所有必要的信息,如:
def hFunc[L <: HList](hlist: L)(implicit h1: Helper1[L], h2: Helper2[L]) {
// here is some code
}
例如,如果你想
reverse
你的
Hlist
和
map
超出您应该使用的结果
Mapper
和
Reverse
这样地:
import shapeless._, shapeless.ops.hlist.{Reverse, Mapper}
object negate extends Poly1 {
implicit def caseInt = at[Int]{i => -i}
implicit def caseBool = at[Boolean]{b => !b}
implicit def caseString = at[String]{s => "not " + s}
}
def hFunc[L <: HList, Rev <: HList](hlist: L)(
implicit rev: Reverse[L]{ type Out = Rev },
map: Mapper[negate.type, Rev]): map.Out =
map(rev(hlist)) // or hlist.reverse.map(negate)
用法:
hFunc(HList(1, true, "String"))
//String :: Boolean :: Int :: HNil = not String :: false :: -1 :: HNil