In Scala I can enforce type equality at compile time. For example:
case class Foo[A,B]( a: A, b: B )( implicit ev: A =:= B )
scala> Foo( 1, 2 )
res3: Foo[Int,Int] = Foo(1,2)
scala> Foo( 1, "2" )
<console>:10: error: Cannot prove that Int =:= java.lang.String.
Is there a way to enforce that type A and type B should be different ?
This is not an answer, just the beginnings of what I could think is an answer. The code below will return either an
Yes
or anNo
depending on whether the types are equal or not, if you ask forimplicitly[AreEqual[A,B]]
. How to go from there to actually making a check I haven't been able to figure out. Maybe the whole approach is doomed, maybe someone can make something out of it. Mind you,implicitly[No[A, B]]
will always return something, one can't use that. :-(Test: