Miles Sabin, unboxed newtypes in Scala
Oct. 9th, 2011 11:34 amhere
The idea is to attach specific types to unboxed values, e.g. you have longs as ids, but an id of what? With this trick you can easily tag values, so you won't be able to mix them.
A bonus point is an example of how one can have binary ops on types.
The idea is to attach specific types to unboxed values, e.g. you have longs as ids, but an id of what? With this trick you can easily tag values, so you won't be able to mix them.
A bonus point is an example of how one can have binary ops on types.
class User class Checkin type Tagged[U] = { type Tag = U } type @@[T, U] = T with Tagged[U] // Thanks to @retronym for suggesting this type alias class Tagger[U] { def apply[T](t : T) : T @@ U = t.asInstanceOf[T @@ U] } def tag[U] = new Tagger[U] // Manual specialization needed here ... specializing apply above doesn't help def tag[U](i : Int) : Int @@ U = i.asInstanceOf[Int @@ U] def tag[U](l : Long) : Long @@ U = l.asInstanceOf[Long @@ U] def tag[U](d : Double) : Double @@ U = d.asInstanceOf[Double @@ U] def fetch[A](id: Int @@ A): A = null.asInstanceOf[A]