Skip to content

Instantly share code, notes, and snippets.

@adamw
Created February 21, 2012 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamw/1878718 to your computer and use it in GitHub Desktop.
Save adamw/1878718 to your computer and use it in GitHub Desktop.
object Example {
// Works
sealed trait Command1[R] // R - return type
case class IntCommand1() extends Command1[Int]
case class StringCommand1() extends Command1[String]
def execute[R](cmd: Command1[R]): R = cmd match {
case IntCommand1() => 5
case StringCommand1() => "x"
}
// Doesn't work; same as above, only difference is the variance annotation
sealed trait Command2[+R] // R - return type
case class IntCommand2() extends Command2[Int]
case class StringCommand2() extends Command2[String]
/*
Compile errors:
error: type mismatch;
found : Int(5)
required: R
case IntCommand2() => 5
*/
def execute[R](cmd: Command2[R]): R = cmd match {
case IntCommand2() => 5
case StringCommand2() => "x"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment