Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Covariance and Contravariance named like that?

I was wondering why the terms Covariance and Contravariance are named like that in programming domain. Usually one hears these terms in Probability theory or Statistics indicating the spread of quantities being measured with respect to the Mean.

  • What is the idea for borrowing these terms from Statistics?
  • How does these terms describe this spread in programming domain?
  • What would be the Mean in programming domain?

I know that Covariance is the ability to assign an expression of a more specific type to a variable of a less specific type but, is there another interpretation for this?
Examples relating both these domains would be helpful.

like image 873
zizouraj Avatar asked Aug 27 '14 14:08

zizouraj


People also ask

What is the meaning of covariance and Contravariance?

Covariance and contravariance are terms that refer to the ability to use a more derived type (more specific) or a less derived type (less specific) than originally specified. Generic type parameters support covariance and contravariance to provide greater flexibility in assigning and using generic types.

What is the difference between covariance and Contravariance in delegates?

Covariance permits a method to have return type that is more derived than that defined in the delegate. Contravariance permits a method that has parameter types that are less derived than those in the delegate type.

What is covariance and Contravariance in generics in Java?

Covariance can be translated as "different in the same direction," or with-different, whereas contravariance means "different in the opposite direction," or against-different. Covariant and contravariant types are not the same, but there is a correlation between them. The names imply the direction of the correlation.


1 Answers

I'm not a mathematician, so I wouldn't try to answer the question my own, but you can find a wonderful explanation to your question on Tomas Petricek's blog.

He explains in detail there how covariance and contravariance in programming are related to pure mathematical category theory.

like image 179
alaendle Avatar answered Sep 27 '22 21:09

alaendle