Covariance and contravariance (computer science)
Many programming language type systems support subtyping. For instance, if the type <span class="n">Cat</span> is a subtype of <span class="n">Animal</span>, then an expression of type <span class="n">Cat</span> should be substitutable wherever an expression of type <span class="n">Animal</span> is used. 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 type. Here, the subtyping relation of the simple types is reversed for the complex types.
- Comment
- enMany programming language type systems support subtyping. For instance, if the type <span class="n">Cat</span> is a subtype of <span class="n">Animal</span>, then an expression of type <span class="n">Cat</span> should be substitutable wherever an expression of type <span class="n">Animal</span> is used. 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 type. Here, the subtyping relation of the simple types is reversed for the complex types.
- Depiction
- Has abstract
- enMany programming language type systems support subtyping. For instance, if the type <span class="n">Cat</span> is a subtype of <span class="n">Animal</span>, then an expression of type <span class="n">Cat</span> should be substitutable wherever an expression of type <span class="n">Animal</span> is used. Variance refers to how subtyping between more complex types relates to subtyping between their components. For example, how should a list of <span class="n">Cat</span>s relate to a list of <span class="n">Animal</span>s? Or how should a function that returns <span class="n">Cat</span> relate to a function that returns <span class="n">Animal</span>? 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 are 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 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 (<span class="n">Cat</span> is covariant to <span class="n">Animal</span>) while contravariance is the quality of being different by being more general (<span class="n">Animal</span> is contravariant to <span class="n">Cat</span>). A programming language designer will consider variance when devising typing rules for language features such as arrays, inheritance, and generic datatypes. 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 type errors can lead to complex typing rules. In order to keep the type system simple and allow useful programs, a language may treat a type constructor as invariant even if it would be safe to consider it variant, or treat it as covariant even though that could violate type safety.
- Is primary topic of
- Covariance and contravariance (computer science)
- Label
- enCovariance and contravariance (computer science)
- Link from a Wikipage to an external page
- www.javac.info/closures-v05.html
- blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx
- tomasp.net/blog/variance-explained.aspx/
- c2.com/cgi/wiki%3FContraVsCoVariance
- Link from a Wikipage to another Wikipage
- Animal shelter
- Array data type
- Bounded quantification
- C++
- Category:Object-oriented programming
- Category:Polymorphism (computer science)
- Category:Type theory
- Category theory
- Closure (computer science)
- Common Lisp Object System
- Covariance and contravariance of functors
- Covariant return type
- C Sharp (programming language)
- D (programming language)
- Dart (programming language)
- Duck typing
- Eiffel (programming language)
- Existential type
- File:AnimalShelter.svg
- File:Inheritance contravariant argument animalshelter.svg
- File:Inheritance covariant argument animalshelter.svg
- File:Inheritance covariant return animalshelter.svg
- File:Java wildcard subtyping.svg
- First-class function
- Function overloading
- Function type
- Generic datatype
- Higher-order function
- Inheritance (computer science)
- Java (programming language)
- Java (Programming language)
- Java Platform, Standard Edition
- John C. Reynolds
- Joshua Bloch
- Kotlin (programming language)
- Liskov substitution principle
- Luca Cardelli
- Martin Odersky
- Method overriding
- Multiple dispatch
- OCaml
- Parametric polymorphism
- Polymorphism (computer science)
- Programming language
- Robustness principle
- Rule of inference
- Sather
- Scala (programming language)
- Subtyping
- Type constructor
- Type inference
- Type system
- Undecidable problem
- Unified Modeling Language
- Variable shadowing
- Wildcard (Java)
- SameAs
- 3Lg73
- Covarianza e controvarianza (informatica)
- Covarianza y contravarianza (ciencias de la computación)
- Kovarianz und Kontravarianz
- m.046c29
- Q362031
- Ковариантность и контравариантность (программирование)
- Коваріантність і контраваріантність (програмування)
- لاتباين وتباين مرافق وتباين معاكس
- هموردایی و پادوردایی (علوم رایانه)
- 共変性と反変性 (計算機科学)
- 协变与逆变
- 공변성과 반공변성 (컴퓨터 과학)
- Subject
- Category:Object-oriented programming
- Category:Polymorphism (computer science)
- Category:Type theory
- Thumbnail
- WasDerivedFrom
- Covariance and contravariance (computer science)?oldid=1120227849&ns=0
- WikiPageLength
- 53081
- Wikipage page ID
- 1104704
- Wikipage revision ID
- 1120227849
- WikiPageUsesTemplate
- Template:Clear
- Template:C sharp
- Template:Java
- Template:More citations needed section
- Template:OCaml
- Template:Type systems