Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:1878718
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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
Something went wrong with that request. Please try again.