Created
January 5, 2012 10:10
-
-
Save machisuji/1564557 to your computer and use it in GitHub Desktop.
My personal Scala lesson of the day - Unions
This file contains hidden or 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
| describe("Unions") { | |
| trait TermUnion[T] | |
| trait Expression { /* ... */ } | |
| object TermUnion { | |
| implicit object DoubleWitness extends Union[Double] | |
| implicit object ExpressionWitness extends Union[Expression] | |
| } | |
| /** Only accepts union types Double and Expression. */ | |
| def read[T: TermUnion](value: T) = value match { | |
| case _: Double => "Double" | |
| case _: Expression => "Expression" | |
| } | |
| read(42d) should equal ("Double") | |
| read(new Expression{}) should equal ("Expression") | |
| // read("Hallo") on the other hand won't even compile, yay! | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment