Martin Odersky's book Scala Programming has 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'm curious about the designation + =. In the Map class, I did not find the + = method. I managed the same behavior in my own example as
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 ask myself why the notation + = is possible. This does not work if, for example, a method of the Foo class is called test. This led me to prefix notation. Is prefix notation + for destination sign (=)? Can anyone explain this behavior?
scala
Steve
source share