问题
I'm working with abstract types, and I'm wondering why this is invalid:
class A {}
class B extends A {}
class X {type T = A}
class Y extends X {override type T = B}
Seeing as B <: A, why can't I assign B to T?
I get this error:
overriding type T in class X, which equals A;
type T has incompatible type
class Y extends X {override type T = B}
Any help would be appreciated.
回答1:
When you say this:
class X {type T = A}
you say: T
is exactly A
or T
is an alias for A
. It can't be anything else, including subtypes of A
.
You probably meant this:
class X {type T <: A}
来源:https://stackoverflow.com/questions/25002876/why-is-this-invalid-scala