开发者

Missing compile/runtime error when inlining

开发者 https://www.devze.com 2023-01-14 22:47 出处:网络
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]{

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 Int开发者_如何学运维eger, 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.


List is covariant, so List[Number] is a supertype of List[Integer]. In the second case, Scala looks at the code and goes, "Huh, bar wants something typed with Number, so let's see if we can get ContraListWrapper to return that. Sure we can--we can tell it it's getting a List[Number] instead of a List[Integer]."

In the first case, the type is already fixed.

You can demonstrate this to yourself by changing the second case to

bar(new ContraListWrapper[Integer](list));

This gives a compile time error as it should, because the compiler is not free to choose the type as Number.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号