In discussion of the type system of a programming language, an operator from types to types is covariant if it preserves the ordering, ≤, of types, which orders types from more generic ones to more specific ones; it is contravariant if it reverses this ordering. If neither of these apply, the operator is invariant. These terms come from category theory.

PropertyValue
p:abstract
  • In discussion of the type system of a programming language, an operator from types to types is covariant if it preserves the ordering, ≤, of types, which orders types from more generic ones to more specific ones; it is contravariant if it reverses this ordering. If neither of these apply, the operator is invariant. These terms come from category theory. In software-engineering terms, this distinction is important in considering argument and return types of methods in class hierarchies. In object-oriented languages such as C++, if class B is a subtype of class A, then all member functions of B must return the same or narrower set of types as A; the return type is said to be covariant. On the other hand, the member functions of B must take the same or broader set of arguments compared with the member functions of A; the argument type is said to be contravariant. The problem for instances of B is how to be perfectly substitutable for instances of A. The only way to guarantee type safety and substitutability is to be equally or more liberal than A on inputs, and to be equally or more strict than A on outputs. Note that not all programmming languages guarantee both properties in every context, and that some are unnecessarily strict; they are said not to support covariance or contravariance in a given context; the behaviour of some programming languages is discussed below. Typical examples: * The operator which constructs array types from element types is usually covariant on the base type: since String ≤ Object then ArrayOf(String) ≤ ArrayOf(Object). Note that this is only correct (i.e. type safe) if the array is immutable; if insert and remove operators are permitted, then the insert operator is covariant (e.g. one can insert a String into an ArrayOf(Object)) and the remove operator is contravariant (e.g. one can remove an Object from an ArrayOf(String)). Since the mutators have conflicting variance, arrays should be invariant on the base type. * A function with a parameter of type T (defined as fun f (x : T) : Integer) can be replaced by a function g (defined as fun g (x : S) : Integer) if T ≤ S. In other words, if g cares less about the type of its parameter, then it can replace f anywhere, since both return an Integer. So, in a language accepting function arguments, g ≤ f and the type of the parameter to f is said to be contravariant. * In the general case, the type of the result is covariant. In object-oriented programming, substitution is also implicitly invoked by overriding methods in subclasses: the new method can be used where the old method was invoked in the original code. Programming languages vary widely on their allowed forms of overriding, and on the variance of overridden methods' types. (en)
  • In der objektorientierten Programmierung bezeichnet man mit Kovarianz, Kontravarianz und Invarianz eine bestimmte Beziehung zwischen den Elementen in einer Unterklasse zu ihren Entsprechungen in der Oberklasse. Den Begriffen liegen die Überlegungen des Ersetzbarkeitsprinzips zugrunde: Objekte der Oberklasse müssen durch Objekte einer ihrer Unterklassen ersetzbar sein. Das bedeutet zum Beispiel, dass die Methoden der Unterklasse mindestens die Parameter akzeptieren müssen, die die Oberklasse auch akzeptieren würde (Kontravarianz). Die Methoden der Unterklasse müssen ebenfalls Werte zurückliefern, die mit der Oberklasse vereinbar sind, also nie allgemeineren Typs sind, als der Rückgabetyp der Oberklasse (Kovarianz). (de)
p:hasPhotoCollection
p:javadocSeProperty
  • Object (en)
  • String (en)
  • java/lang (en)
p:reference
p:wikiPageUsesTemplate
p:wikipage-de
rdfs:comment
  • In discussion of the type system of a programming language, an operator from types to types is covariant if it preserves the ordering, ≤, of types, which orders types from more generic ones to more specific ones; it is contravariant if it reverses this ordering. If neither of these apply, the operator is invariant. These terms come from category theory. (en)
  • In der objektorientierten Programmierung bezeichnet man mit Kovarianz, Kontravarianz und Invarianz eine bestimmte Beziehung zwischen den Elementen in einer Unterklasse zu ihren Entsprechungen in der Oberklasse. (de)
rdfs:label
  • Covariance and contravariance (computer science) (en)
  • Kovarianz (Informatik) (de)
skos:subject
foaf:page
p:redirect