Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is overriding an already implemented abstract type not possible?

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?

like image 379
Tim Friske Avatar asked Jan 07 '12 16:01

Tim Friske


1 Answers

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 ?

like image 56
Didier Dupont Avatar answered Sep 21 '22 19:09

Didier Dupont