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 X
- scala> 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 legal
- class 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 class
- def writeObject(out : Output[Object]) = out.write("hello")
- // Now this is illegal as it should be
- scala> 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.
Hi Jesse,
ReplyDelete>I can almost here
I can almost hear,...
G'day,
Eric.
In the book, Java Generics by Maurice Naftalin, Philip Wadler published by O'Reilly has a 'get and put principle' which stated:
ReplyDelete"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"
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
ReplyDelete