self ( this, , : " B A [ยง5.1, Scala ]), .
:
trait A { val v = "a" }
trait B { this: A =>
var v = "b"
}
new A with B {}
<console>:9: error: overriding value v in trait A$class of type java.lang.String;
variable v in trait B$class of type java.lang.String needs `override' modifier
new A with B {}
, B, .
trait A { val v = "a" }
trait B { this:A => override val v = "b" }
new A with B {}
A s v B. ( , new B with A {} , B .) , val, var - var.
, self-type . B, this , , . . B, B , .
trait B { this =>
val v = "b"
trait C {
val v = "c"
println(this.v)
}
new C {}
}
new B {}
vs this:
trait B { self =>
val v = "b"
trait C {
val v = "c"
println(this.v)
println(self.v)
}
new C {}
}
new B {}
( - this .)