Type variance

Type variance

top: fix capitalization for last edit

← Previous revision Revision as of 15:51, 23 April 2026
Line 1: Line 1:
{{Short description|Programming language concept}}
{{Short description|Programming language concept}}
{{Type systems}}
{{Type systems}}In [[computer programming]], '''Type variance''' is the relationship between subtypes of a [[composite type]] (e.g. {{Code|List[Int]}}) and the subtypes of its components (e.g. {{Code|Int}}). A language's chosen variance determines the relationship between, for example, a [[list (programming)|list]] of {{C sharp|Cat}}s and a list of {{C sharp|Animal}}s, or a [[Function (computer science)|function]] [[return value|return]]ing {{C sharp|Cat}} and a function returning {{C sharp|Animal}}.
In [[computer programming]], '''type variance''' is the relationship between subtypes of a [[composite type]] (e.g. {{Code|List[Int]}}) and the subtypes of its components (e.g. {{Code|Int}}). A language's chosen variance determines the relationship between, for example, a [[list (programming)|list]] of {{C sharp|Cat}}s and a list of {{C sharp|Animal}}s, or a [[Function (computer science)|function]] [[return value|return]]ing {{C sharp|Cat}} and a function returning {{C sharp|Animal}}.


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. 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 many programming languages, for example, "list of Cat" will be 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. 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.
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. 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 many programming languages, for example, "list of Cat" will be 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. 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.