is the + in += on a Map a prefix operator of =?
- by Steve
In the book "Programming in Scala" from Martin Odersky there is a simple example in the first chapter:
var capital = Map("US" -> "Washington", "France" -> "Paris")
capital += ("Japan" -> "Tokyo")
The second line can also be written as
capital = capital + ("Japan" -> "Tokyo")
I am curious about the += notation. In the class Map, I didn't found a += method. I was able to the same behaviour in an own example like
class Foo() {
def +(value:String) = {
println(value)
this
}
}
object Main {
def main(args: Array[String]) = {
var foo = new Foo()
foo = foo + "bar"
foo += "bar"
}
}
I am questioning myself, why the += notation is possible. It doesn't work if the method in the class Foo is called test for example. This lead me to the prefix notation. Is the + a prefix notation for the assignment sign (=)? Can somebody explain this behaviour?