Pages that I expanded

edit


Sandbox

edit

In Computer Science, variance properties describe how some type operators (i.e., functions acting on types in the type system of a given programming language) behave relatively to some order relation for the types, which is usually the subtype order defined by inheritance in object-oriented programming language. A type operator is said to be covariant if it preserves this order, contravariant if it reverses it, and invariant otherwise.

Variance properties can be specified in the programming language for some type operators, most often for operators allowing the construction of new types from known ones (such as arrays, pointers, etc.), or for type qualifiers (such as const). On the other hand, some programming language allows the programmer to create such type operators via generic programming and to specify their variance properties.

The concept of variance is also applied to operators that do not appear explicitly in the programming language but reflects how the compiler or interpreter should work, such as getting the return type of a given function from the type of one of its arguments; or getting from a given class the type of its member with a given name. When the variance of such artificial type operators is defined in the programming language, it sets some aspects of the language, such as the acceptable signatures of a method overriding another one.

When a variance property is set, it usually allows programs to be more concise (by reducing the number of explicit type conversions) and easier to understand (by allowing the reuse of identifiers when it makes sense). It can also help the developer to understand some high-level concepts of the language.

(but other order relations can be considered, such as the more general one defined by the Liskov substitution principle.)