Why is this invalid Scala?

孤人 提交于 2021-02-11 06:46:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!