self- this is just an alias thisin the object in which it is declared, and can be any valid identifier (but not this, otherwise an alias is not created). Therefore, selfit can be used to refer thisto an external object from within the internal object, where thisotherwise it means something else. Perhaps this example will clarify the situation:
trait Outer { self =>
val a = 1
def thisA = this.a
def selfA = self.a
object Inner {
val a = 2
def thisA = this.a
def selfA = self.a
}
}
object Outer extends Outer
Outer.a
Outer.thisA
Outer.selfA
Outer.Inner.a
Outer.Inner.thisA
Outer.Inner.selfA
source
share