I am using Shapeless and have the following method to compute the difference between two HLists:
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")
}
Since both parameters to the method take an H
, I would expect HLists of different types to not compile here. For example:
diff("a" :: HNil, 1 :: 2 :: HNil)
Shouldn't compile but it does, and it produces a runtime error: java.lang.RuntimeException: something went very wrong
. Is there something I can do to the type parameters to make this method only accept two sides with identical types?
lst1
orlst2
is empty, which might very well explain your error. - Régis Jean-GillesHList
trait is unparameterized, and so in your method callH
is just resolved toHlist
(which is indeed a supertype of anyHlist
irrespective of the concrete element types). See my answer. - Régis Jean-Gilles