Invariance

val catContainer: Container[Cat] = new Container(Cat("Felix"))
val animalContainer: Container[Animal] = catContainer
animalContainer.setValue(Dog("Spot"))
val cat: Cat = catContainer.getValue // Oops, we'd end up with a Dog assigned to a Cat

Generic classes in Scala are invariant by default. This means that they are neither covariant nor contravariant.

Invariance → is a → Variance.

Related concepts

Invariance — Structure map

Clickable & Draggable!

Invariance — Related pages: