Covariance and contravariance (computer science): Difference between revisions

Content deleted Content added
top: The lead doesn't need to be rewritten IMO. There are some things that could be improved, I suppose, but nothing major. Please be more specific if needed.
Reworded the second paragraph, because the first sentence's structure, built around "how … relates to …", breaks its definition into separated halves, and readers must infer (due to the vagueness of "how") what type of thing "variance" might be. The next two sentences, both rhetorical questions, are converted to a single sentence of statements. NOTE: This edit would be significantly improved by enumerating variance types in sentence 1, and stating their effects on the examples in sentence 2.
Line 3:
Many [[programming language]] [[type system]]s support [[subtyping]]. For instance, if the [[type (computer science)|type]] {{C sharp|Cat}} is a subtype of {{C sharp|Animal}}, then an [[expression (computer science)|expression]] of type {{C sharp|Cat}} [[Liskov_substitution_principle|should be substitutable]] wherever an expression of type {{C sharp|Animal}} is used.
 
'''Variance''' is howthe subtypingcategory of possible relationships between more [[composite type|complex types]] relates to subtyping betweenand their components' subtypes. ForA examplelanguage's chosen variance determines the relationship between, howfor shouldexample, a [[list (programming)|list]] of {{C sharp|Cat}}s relate toand a list of {{C sharp|Animal}}s? Or how, shouldor a [[Function (computer science)|function]] that [[return value|return]]sing {{C sharp|Cat}} relate toand a function that returnsreturning {{C sharp|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 is preserved for the complex types.