Skip to content

Instantly share code, notes, and snippets.

@quelgar
Created July 16, 2015 06:27
Show Gist options
  • Save quelgar/f6303fef89c0ea2d12ce to your computer and use it in GitHub Desktop.
Save quelgar/f6303fef89c0ea2d12ce to your computer and use it in GitHub Desktop.
Tagged types experiment
bject Tag {
type Tagged[T] = {
type Tag = T
}
type @@[A, T] = A with Tagged[T]
final class NewType[A](val underlying: A) extends AnyVal {
}
// final class Tagger[U] {
// def apply[T](t: T): T @@ U = t.asInstanceOf[T @@ U]
// }
//
// def tag[U] = new Tagger[U]
final implicit class RichAnyRef[A <: AnyRef](val underlying: A) extends AnyVal {
def tag[T]: A @@ T = underlying.asInstanceOf[A @@ T]
}
final implicit class RichInt(val underlying: Int) extends AnyVal {
def tag[T]: Int @@ T = underlying.asInstanceOf[Int @@ T]
}
def tag[T](i: Int): Int @@ T = i.asInstanceOf[Int @@ T]
final class Foo
final class Bar
val f = 5.tag[Foo]
val b = 6.tag[Bar]
val a = tag[Foo](0)
val x = 1 - f
val y = f - b
val fs: List[Int @@ Foo] = List(f)
val bs: List[Int @@ Bar] = List(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment