Created
June 9, 2011 18:06
-
-
Save aboisvert/1017337 to your computer and use it in GitHub Desktop.
Union Type + Specialized
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
object Union { | |
type ¬[A] = A => Nothing | |
type ¬¬[A] = ¬[¬[A]] | |
type ∨[T, U] = ¬[¬[T] with ¬[U]] | |
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (T ∨ U) } | |
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