Covariance and Contravariance: a fresh look at an old issue [pdf]
Covariance and contravariance are wonderful ways to notice key OO design problems.  Unfortunately it is very hard to learn to think that way.<p>Consider an OO language with classes and inheritance.  Each class is a type.  (There may be types that aren&#x27;t classes, for example Java an interface also represents a type.  The equivalent in a dynamic language with duck typing is &quot;all objects that satisfy this contract&quot;.)  An object belongs to the type of its class, and every class you inherit from.  (For instance an integer is an integer, a number, and an object.)<p>So far, so good.<p>Covariance occurs when we can use objects of any subtype freely.  For instance we can insert integers into a list of numbers.<p>Contravariance occurs when we can allow an object to be of some supertype freely.  For instance it is safe to assume that numbers from our list of numbers are objects.<p>The problem is that we can almost never do both.  For example in Java you can put integers into a list of numbers, but you can&#x27;t read numbers out of a list of numbers and assume that they will be integers!  (Not even if you only put integers in - the type system won&#x27;t let you do it.)<p>So, which do we want?  Well, sometimes one, and sometimes the other.  For example the Liskov substitution principle says that an object of a subtype should be usable anywhere we can use an object of the original type.  Which means that if we override a method in a subclass, we are OK if we change the method&#x27;s signature to accept a supertype, but are breaking the rule if we change it to require a subtype of the original.<p>Unfortunately it sometimes makes sense to have a subtype override a method and require a subtype be passed in.  The paper offers a graphics example involving colors.<p>When we have this, we have 3 options.  1) Disallow it because the type system can&#x27;t easily guarantee that things won&#x27;t break.  (Static languages like Java mostly do this.)  2) Assume that the programmer isn&#x27;t an idiot then throw run-time errors if the programmer was.  (Most dynamic languages do that.)  3) Build a sophisticated type system that can figure things out and reason out problem cases in a clever way.  (This is what the author would like language designers to do.)<p>Unfortunately for the author, there is a chicken and egg problem here.  Few programmers understand the sophisticated type system required for the reasoning solution, or can understand the weird errors that the type system can give you to say why it won&#x27;t let you do something stupid.  So developers shy away from languages that provide such types.  Therefore there is little demand for languages that provide it.<p>As a result language designers have little reason to do anything other than either simple type systems with easy to understand checks, or dynamic dispatch with run-time errors.  Which is a frustration to people who have put effort into how to have clever type systems that provide both programming flexibility and automatically catch common classes of errors.