Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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