我使用Shapeless,并使用以下方法来计算两个HList之间的差异:
def diff[H <: HList](lst1: H, lst2:H):List[String] = (lst1, lst2) match {
case (HNil, HNil) => List()
case (h1::t1, h2::t2) if h1 != h2 => s"$h1 -> $h2" :: diff(t1, t2)
case (h1::t1, h2::t2) => diff(t1, t2)
case _ => throw new RuntimeException("something went very wrong")
}
由于该方法的两个参数都采用H
,因此我希望不同类型的HList不会在这里编译。例如:
diff("a" :: HNil, 1 :: 2 :: HNil)
不应该编译,但它确实编译了,并且会产生运行时错误:java.lang.RuntimeException: something went very wrong
。我可以对类型参数做些什么,使这个方法只接受具有相同类型的两边吗?
其他答案没有真正解决的一件事是,这完全是一个类型推理问题,只需将参数列表一分为二即可解决:
def diff[H <: HList](lst1: H)(lst2: H): List[String] = (lst1, lst2) match {
case (HNil, HNil) => List()
case (h1::t1, h2::t2) if h1 != h2 => s"$h1 -> $h2" :: diff(t1)(t2)
case (h1::t1, h2::t2) => diff(t1)(t2)
case _ => throw new RuntimeException("bad!")
}
这给了我们想要的:
scala> diff("a" :: HNil)(1 :: 2 :: HNil)
<console>:15: error: type mismatch;
found : shapeless.::[Int,shapeless.::[Int,shapeless.HNil]]
required: shapeless.::[String,shapeless.HNil]
diff("a" :: HNil)(1 :: 2 :: HNil)
^
这是有效的(即不会不适当地编译,然后在运行时崩溃),因为Scala对方法的类型推断是基于每个参数列表的。如果lst1
和lst2
在同一个参数列表中,则H
将被推断为它们的最小上界,这通常不是您想要的。
如果将lst1
和lst2
放在单独的参数列表中,那么编译器一看到lst1
就会决定什么是H
。如果lst2
没有相同的类型,它就会爆炸(这就是我们的目标)。
您仍然可以通过显式地将H
设置为HList
来打破这一点,但恐怕这是您自己的想法。
不幸的是,基本HList
特征是未参数化的,因此在方法调用中,H
仅被解析为Hlist
(它实际上是任何Hlist
的超类型,与具体元素类型无关)。为了解决这个问题,我们必须稍微改变定义,转而依赖于广义类型约束:
def diff[H1 <: HList, H2 <: HList](lst1: H1, lst2: H2)(implicit e: H1 =:= H2): List[String] = (lst1, lst2) match {
case (HNil, HNil) => List()
case (h1::t1, h2::t2) if h1 != h2 => s"$h1 -> $h2" :: diff(t1, t2)
case (h1::t1, h2::t2) => diff(t1, t2)
case _ => throw new RuntimeException("something went very wrong")
}
让我们检查一下:
scala> diff("a" :: HNil, 1 :: 2 :: HNil)
<console>:12: error: Cannot prove that shapeless.::[String,shapeless.HNil] =:= shapeless.::[Int,shapeless.::[Int,shapele
diff("a" :: HNil, 1 :: 2 :: HNil)
^
scala> diff("a" :: HNil, "b" :: HNil)
res5: List[String] = List(a -> b)
scala> diff("a" :: 1 :: HNil, "b" :: 2 :: HNil)
res6: List[String] = List(a -> b, 1 -> 2)
现在我们仍然可以"欺骗"并显式地将H1和H2设置为HList
,然后我们回到原点。
scala> diff[HList, HList]("a" :: HNil, 1 :: 2 :: HNil)
java.lang.RuntimeException: something went very wrong
at .diff(<console>:15)
at .diff(<console>:13)
不幸的是,我认为这不是一个容易解决的问题(当然是这样,但我没有一个快速的解决方案)。
我可以提供一个更严格的变体,它不能被显式类型参数欺骗。
object diff {
class Differ[T <: HList](val diff: (T, T) => List[String])
def apply[T <: HList](l1: T, l2: T)(implicit differ: Differ[T]): List[String] = differ.diff(l1, l2)
implicit object NilDiff extends Differ[HNil]((_, _) => Nil)
implicit def ConsDiff[H, T <: HList : Differ] = new Differ[H :: T]({
case (h1 :: t1, h2 :: t2) if h1 != h2 => s"$h1 -> $h2" :: diff(t1, t2)
case (h1 :: t1, h2 :: t2) => diff(t1, t2)
})
}
它肯定比上面的要复杂得多,我试过使用多态函数,但最终无法编译正确的递归。