按模式排序HList

时间:2016-11-24 10:15:41

标签: scala shapeless hlist

假设我有一个hlist

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

我有一个模式

type Pattern = Int :: Boolean :: HNil

如何使用模式重新排序现有的hlist?即。

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

1 个答案:

答案 0 :(得分: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