次のコードは Scala 2 でコンパイルされません (2.13.7 および Scala 2.12.15 でテスト済み):
trait AttributeBase
trait Attribute extends AttributeBase {
def name: String
}
trait Base {
def attribute: AttributeBase
}
trait Derived { self: Base =>
def attribute: Attribute
def name = attribute.name
}
コードは Scala 3 で問題なくコンパイルされます。Scala 2 では次のエラーが発生します。
値の名前は AttributeBase のメンバーではありません
extends
self 型の代わりに を使用すると、正常にコンパイルされます。
trait Derived extends Base {
別の可能な回避策は次のとおりです。
def name = (this:Derived).attribute.name
の型が であるDerived.attributed
と見なされ、AttributeBase
ではないのはなぜAttribute
ですか? これは Scala コンパイラのバグですか、それとも Scala 2 の制限ですか?