Skip to content

Instantly share code, notes, and snippets.

@aboisvert
Created June 9, 2011 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aboisvert/1017337 to your computer and use it in GitHub Desktop.
Save aboisvert/1017337 to your computer and use it in GitHub Desktop.
Union Type + Specialized
object Union {
type ¬[A] = A => Nothing
type ¬¬[A] = ¬[¬[A]]
type [T, U] = ¬[¬[T] with ¬[U]]
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (TU) }
def size[@specialized(Int) T : (Int || String)#λ](t : T) = t match {
case i : Int => i
case s : String => s.length
}
def main(args: Array[String]) {
println(Union.size(2))
println(Union.size("foo"))
}
}
/*
boisvert@smudge:~/scala/union-type$ javap Union$
Compiled from "Union.scala"
public final class Union$ extends java.lang.Object implements scala.ScalaObject{
public static final Union$ MODULE$;
public static {};
public int size(java.lang.Object, scala.Predef$$less$colon$less);
public void main(java.lang.String[]);
public int size$mIc$sp(int, scala.Predef$$less$colon$less); // Yay! @specialized works!
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment