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?
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
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With