## Saturday, March 27, 2010

### Contravariance

Continuing on with variance and type parameters, this topic will discuss contravariance. See the post In- and Co- variance of type parameters for the intro material required for this topic.

Covariant parameters allow for an additional dimension of type compatibility:
`val l : List[Object] = List("this is legal")`
Contravariance provides the opposite:
`// If the type parameter of list was contravariant this would be legal:val l : List[String] = List(new Object())`
As covariance is indicated by a '+' before the type contravariance is indicated by a '-'
`scala> class X[-A]defined class Xscala> val l : X[String] = new X[Object]l: X[String] = X@66201d6d`
I can almost hear the "cool... but why?". Following the lead in the Programming In Scala book. Consider OutputStream first and a method in a Collection second. (The following code is illegal but consider it)
`class Output[+A] {def write(a : A) = () /*do write*/ }def writeObject(out : Output[Object]) = out.write("hello")/*Uh oh you this only is for outputting lists not Objects (certainly not the String that is actually written)Runtime error for sure!*/writeObject(new Output[List[String]])`
The previous example (if it would compile) would explode because an Output that can only write lists is passed to the method. In the example a String is written to the Output object. The Output[List[String]] cannot handle that.

Fortunately the compiler sees the definition of the class and recognizes this is an error waiting to happen and makes it illegal:
`scala> class Output[+A] {def write(a : A) = () /*do write*/ }< console>:5: error: covariant type A occurs in contravariant position in type A of value a       class Output[+A] {def write(a : A) = () /*do write*/ }                                   ^`
Consider the implications of making A contravariant?
`// The definition of object is now legalclass Output[-A] {def write(a : A) = () /*do write*/ }// this is now a safe method definition since the parameter of Output must be a Object or a super classdef writeObject(out : Output[Object]) = out.write("hello")// Now this is illegal as it should bescala> writeObject(new Output[List[String]])< console>:8: error: type mismatch; found   : Output[List[String]] required: Output[java.lang.Object]       writeObject(new Output[List[String]])       // this is legal... scala> writeObject(new Output[Any])`
In this example Output[Any] can be passed to the method. This makes sense. If the Output object knows how to write Any oject then it knows how to write an Object; its all good.

1. Hi Jesse,

>I can almost here
I can almost hear,...

G'day,

Eric.

2. In the book, Java Generics by Maurice Naftalin, Philip Wadler published by O'Reilly has a 'get and put principle' which stated:

"Use an extends wildcard when you only get values out of a structure, use a super wildcard when you only put values into a structure, and don't use a wildcard when both get and put"

Can that be translated the same to scala?

"Use an [+] wildcard when you only get values out of a structure, use a [-] wildcard when you only put values into a structure, and don't use a wildcard when both get and put"

3. There something called existential types that Martin was forced to add specifically to support those constructs. He has said he doesn't like existential types too much so they are rarely used, primarily only for Java interoperability