Scala: overriding type member with bounds

爱⌒轻易说出口 提交于 2019-12-24 03:25:51

问题


I've narrowed down my issue to the following minimal (non-)working example:

class Z
trait A[E <: Z] { type T[X <: E] <: A[X] }
trait B[E <: Z] extends A[E] { type T[X <: E] <: B[X] }
trait C[E <: Z, F[X <: Z] <: C[X, F]] extends A[E] { type T[X <: E] = F[X] }
class D[E <: Z] extends B[E] with C[E, D]

It is sort of a higher-kinded F-bounded polymorphism (I'm using T[E] as the return value of some methods). When I try to compile this code I get:

error: overriding type T in trait B with bounds[X <: E] <: B[X];
 type T in trait C, which equals [X <: E]D[X] has incompatible type

However, given that D is a sub-type of B and that the same E is passed to B and C, the types shouldn't be incompatible. Martin Odersky explained in this question that when merging a member in a mixin, there are two rules: 1) concrete over abstract and 2) linearisation order. In this case both are "concrete" types so we go with 2). But the linearisation order says that C should win over B, so I'm left wondering what is it that I'm getting wrong here.

来源:https://stackoverflow.com/questions/30118608/scala-overriding-type-member-with-bounds

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