trait A {
type Foo
def bar: Foo = B(this).bar
}
case class B(a: A) extends A {
type Foo = a.Foo
}
我知道这个例子有些人为,但是不应该编译吗?我收到以下错误:
<console>:9: error: type mismatch;
found : _5.a.Foo where val _5: B
required: A.this.Foo
def bar: Foo = B(this).bar
我意识到我的例子很奇怪,但是应该清楚,Foos是相同的,不是吗?因为“这”是定义Foo的路径!
我只需要强制转换吗?
最佳答案
以下工作正常:
trait A {
type Foo
def bar: Foo = B(this).bar
}
def B(a: A): A { type Foo = a.Foo } = new A {
type Foo = a.Foo
}
因此,看起来编译器在跟踪
B
的Foo
是什么方面还不够聪明。也许您可以说服编译器团队中的某人这是一个错误,但我不会屏住呼吸。关于scala - 尽管应该清楚,Scala无法证明路径依赖类型是相等的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31143041/