Apocalisp has a great series on Type Level Programming with Scala. At some point the question came up whether it is possible to determine equality of types at run time by having the compiler generate types representing true and false respectively. Here is what I came up with.
trait True { type t = True } trait False { type t = False } case class Equality[A] { def check(x: A)(implicit t: True) = t def check[B](x: B)(implicit f: False) = f } object Equality { def witness[T] = null.asInstanceOf[T] implicit val t: True = null implicit val f: False = null } // Usage: import Equality._ val test1 = Equality[List[Boolean]] check witness[List[Boolean]] implicitly[test1.t =:= True] // Does not compile since tt is True // implicitly[test1.t =:= False] val test2 = Equality[Nothing] check witness[AnyRef] // Does not compile since ft is False // implicitly[test2.t =:= True] implicitly[test2.t =:= False]
Admittedly this is very hacky. For the time being I don’t see how to further clean this up. Anyone?