Scala: illegal inheritance; self-type Y does not conform to X's selftype SELF

耗尽温柔 提交于 2019-11-30 17:31:25

Self type is more akin to generic constraint than to inheritance. With class C[A <: B], the constraint must be repeated all along in subclasses : class D[A <: B] extends C[A]. The constraint must be repeated until it is satisfied, that is until you have chosen an actual parameter type which indeed satisfies <: B. Same for the self type. Writing self: A => does not makes your type extend A. It ensures that it will ultimately have to be mixed in with A before it is actually instantiated.

On the opposite, when you extend Comparable, you have made your class a Comparable, not put a constraint for later on. But the fact that you need to implement compareTo has still to be repeated all along with abstract until you actually implement it.

Certainly the compiler could do without repeating <: B, self: A =>, and abstract, the info is available to it. This is language designer choice. At least, having to repeat self: A => is not different from the rules everywhere else.

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