Covariance and contravariance

Topic | v1 | created by jjones |

Many programming language type systems support subtyping. For instance, if the type Cat is a subtype of Animal, then an expression of type Cat should be substitutable wherever an expression of type Animal is used. Variance refers to how subtyping between more complex types relates to subtyping between their components. For example, how should a list of Cats relate to a list of Animals? Or how should a function that returns Cat relate to a function that returns Animal? Depending on the variance of the type constructor, the subtyping relation of the simple types may be either preserved, reversed, or ignored for the respective complex types. In the OCaml programming language, for example, "list of Cat" is a subtype of "list of Animal" because the list type constructor is covariant. This means that the subtyping relation of the simple types are preserved for the complex types.


e.g. Dart generics variance

Covariant generics fit a common intuition that programmers have, and very often this intuition is cor...

subtopic of Programming language

A programming language is a formal language comprising a set of instructions that produce various kin...

e.g. .NET Covariance and Contravariance

Covariance and contravariance are terms that refer to the ability to use a more derived type (more sp...

Edit details Edit relations Attach new author Attach new topic Attach new resource