Hi,
I have the following classes/traits in Scala
trait Write[-T] {
def add(elem : T);
}
class ContraListWrapper[T] (var list : List[T]) extends Write[T] {
def add(elem : T) = {
list = elem :: list
}
}
def bar(list : Write[Number]) = {}
Invoking the method with a List of Object or list of Numbers works, thanks to contravariance of the Write trait.
var list : List[Number] = Nil;
var wlist = new ContraListWrapper(list);
bar(wlist);
var list : List[Object] = Nil;
var wlist = new ContraListWrapper(list);
bar(wlist);
When I invoke bar with a list of Integer, I receive a compile error in Scala. This is expected; Integer not a supertype of Number (but subtype)
var list : List[Integer ] = new Integer(1) :: Nil;
var wlist = new ContraListWrapper(list);
bar(wlist); //error: type mismatch;
//found : contra.this.ContraListWrapper[Integer]
//required: contra.this.Write[Number]
But when I inline a variable declaration of list of Integer, the compile error is gone and it even seems working. (I can added element to the list in the method bar)
var list : List[Integer] = new Integer(1) :: Nil;
bar(new ContraListWrapper(list)); //no compile- nor runtime error
Edit: answers thx to Rex Kerr in bold
1) How it this possible? First type Integer is chosen, with inlining Number
2) Why isn't this the case without the inlining? Scala can take appropriate type
3) Why I wont event get a runtime error? Because Lists are covariant
Ps. I know I can get contravariance without the trait and wrapper, and with a bound.