When I look at the code of scala.collection.TraversableLike
I see that it starts with self =>
What does it mean?
This is usually used as an alias to "this" variable. Why do you even need this? Consider a simple example:
class A{
self=>
def generate = 5
class B{
def generate:Int = {
self.generate+5
}
}
def o = (new B).generate
}
val a = new A
a.o
If you change "self" to "this" you will get StackOverflowException. Why? Because "this.generate" inside the nested class refers to the method "generate" of class B. So in order to get access to the method "generate" of class A we created an alias to "this". You could have also wrote:
A.this.generate
instead. And it works too.
You might me also interested in this article. http://www.markthomas.info/blog/92
It's something called self-type annotation
and allows two things:
this
(helpful in nested classes or traits, where this
would give you the access to the inner class, and self
would give you the access to the outer class),this
(it works very similar to
inheritance then, you can see examples and differences here)