Scala Language Type Variance Invariance


Example

By default all type parameters are invariant - given trait A[B], we say that "A is invariant on B". This means that given two parametrizations A[Cat] and A[Animal], we assert no sub/superclass relationship between these two types - it does not hold that A[Cat] <: A[Animal] nor that A[Cat] >: A[Animal] regardless of the relationship between Cat and Animal.

Variance annotations provide us with a means of declaring such a relationship, and imposes rules on the usage of type parameters so that the relationship remains valid.