Why is overriding an already implemented abstract type not possible?

1.1k views Asked by At

Given the following code:

class A {

  class B

  type C <: B

  trait D

}

class E extends A {

  type C = B

}

class F extends E {

  override type C = B with D

}

Why does the Scala IDE's presentation compiler within the Eclipse Indigo IDE complain with the error message overriding type C in class E, which equals F.this.B; type C has incompatible type?

After all class "B" is only "amended" with trait "D" and thus the two type definitions are of the same base type, which is "B". Hence compatible type definitions.

The code below works. I consider the rules for type assignment similiar to variable assignment, such as:

class Foo

trait Bar

val a: Foo =  new Foo

val fooWithBar: Foo = new Foo with Bar

Is my understanding wrong?

1

There are 1 answers

3
Didier Dupont On BEST ANSWER

They are not compatible, type C might be used in a contravariant position

class E extends A {
  type C = B
  def f(c: C)
}


class F extends E {
  override type C = B with D 
  def f(c: ???)
}

Complement given e: E, you are allowed to call e.f(new B). What if e was val e = new F ?