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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With