1

假设我有一个 hlist

val hlist: Boolean :: Int :: HNil = true :: 42 :: HNil

我有一个模式

type Pattern = Int :: Boolean :: HNil

如何使用该模式重新排序现有的 hlist?IE

reorder[Int :: Boolean :: HNil](hlist) // 42 :: true :: HNil
4

1 回答 1

4

您正在寻找align和相应的Align类型类shapeless.ops.hlist

scala> import shapeless._
import shapeless._

scala> val hlist: Boolean :: Int :: HNil = true :: 42 :: HNil
hlist: shapeless.::[Boolean,shapeless.::[Int,shapeless.HNil]] = true :: 42 :: HNil

scala> type Pattern = Int :: Boolean :: HNil
defined type alias Pattern

scala> hlist.align[Pattern]
res0: Pattern = 42 :: true :: HNil
于 2016-11-24T10:42:35.590 回答