Created
October 11, 2017 19:50
-
-
Save steinybot/75078e91784515ec4a2ce73e945a870a to your computer and use it in GitHub Desktop.
How to convince the compile that the variance is safe
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 FixingVarianceErrors { | |
trait InvariantThing[T] | |
trait CovariantThing[+T] { | |
// Fails to compile with error: | |
// Error:(12, 9) covariant type T occurs in invariant position in | |
// type => FixingVarianceErrors.InvariantThing[T] of method thing1 | |
def thing1: InvariantThing[T] | |
protected[this] def thing2: InvariantThing[T] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat little trick I noticed being used in Scala collections. I had no idea that this was even possible.