Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unboxed newtype in Scala (with sugar)
class User
class Checkin
type Tagged[T] = { type Tag = T }
type @@[T, U] = T with Tagged[U]
def tag[T] = new {
def apply[U](u : U) : U @@ T = u.asInstanceOf[U @@ T]
}
def fetch[A](id: Int @@ A): A = null.asInstanceOf[A]
def main(args: Array[String]): Unit = {
val id = tag[Checkin](10)
fetch[Checkin](id) // Compiles
// fetch[User](id) // Does not compile
val ids = tag[User](1) :: tag[User](2) :: tag[User](3) :: Nil
val users : List[Int @@ User] = ids // Compiles
// val checkins : List[(Int with Tagged[Checkins])] = ids // Does not compile
}
@paulp

This comment has been minimized.

Copy link

commented Sep 21, 2011

Maybe it doesn't compile because there is no "Checkins" type. (Ha ha.)

@milessabin

This comment has been minimized.

Copy link

commented Sep 21, 2011

@paulp Oops ... corrected in the original (it still doesn't compile ;-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.