/gist:8fe2c19e4431e817015b Secret
Created
September 21, 2011 20:25
-
-
Save retronym/8fe2c19e4431e817015b to your computer and use it in GitHub Desktop.
Unboxed newtype in Scala (with sugar)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe it doesn't compile because there is no "Checkins" type. (Ha ha.)