Skip to content

Instantly share code, notes, and snippets.

@machisuji
Created January 5, 2012 10:10
Show Gist options
  • Select an option

  • Save machisuji/1564557 to your computer and use it in GitHub Desktop.

Select an option

Save machisuji/1564557 to your computer and use it in GitHub Desktop.
My personal Scala lesson of the day - Unions
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