Skip to content

Instantly share code, notes, and snippets.

@steinybot
Created October 11, 2017 19:50
Show Gist options
  • Save steinybot/75078e91784515ec4a2ce73e945a870a to your computer and use it in GitHub Desktop.
Save steinybot/75078e91784515ec4a2ce73e945a870a to your computer and use it in GitHub Desktop.
How to convince the compile that the variance is safe
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]
}
}
@steinybot
Copy link
Author

Neat little trick I noticed being used in Scala collections. I had no idea that this was even possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment