Created
February 21, 2012 20:30
-
-
Save adamw/1878718 to your computer and use it in GitHub Desktop.
This file contains 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
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