Covariance and contravariance (computer science): Difference between revisions

Content deleted Content added
top: WP:REFERS. Bold "Invariant". Add a bunch of links. Grammar.
Vilhelm.s (talk | contribs)
this paragraph was just wrong
Line 8:
 
On the other hand, "function from Animal to String" is a subtype of "function from Cat to String" because the function type constructor is '''contravariant''' in the [[parameter (computer science)|parameter]] type. Here, the subtyping relation of the simple types is reversed for the complex types.
 
In other words, covariance is the quality of being different by being more specific ({{C sharp|Cat}} is covariant to {{C sharp|Animal}}) while contravariance is the quality of being different by being more general ({{C sharp|Animal}} is contravariant to {{C sharp|Cat}}).
 
A programming language designer will consider variance when devising [[typing rule]]s for language features such as [[Array (data type)|array]]s, [[Inheritance (object-oriented programming)|inheritance]], and [[generic datatype]]s. By making type constructors covariant or contravariant instead of '''invariant''', more programs will be accepted as well-typed. On the other hand, programmers often find contravariance unintuitive, and accurately tracking variance to avoid [[runtime errors|runtime type errors]] can lead to complex typing rules.