Covariance and Contravariance (Programming)
The teacher introduced the Contravariance Problem when talking about the Liskov Substitution Principle.
Resource
Suppose A and B are types, and I<U> denotes application of a type constructor I with type argument U. Within the type system of a programming language, a typing rule for a type constructor I is:
- covariant if it preserves the ordering of types (≤), which orders types from more specific to more generic: If
A ≤ B, thenI<A> ≤ I<B>; - contravariant if it reverses this ordering: If
A ≤ B, thenI<B> ≤ I<A>; - bivariant if both of these apply (i.e., if
A ≤ B, thenI<A> ≡ I<B>);[1] - variant if covariant, contravariant or bivariant;
- invariant or nonvariant if not variant.