Search code examples
scalainheritancepolymorphismabstract-type

Why is this invalid Scala?


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.


Solution

  • 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}