This is not an implicit conversion - it is discoloration. Record:
x += 1
desugars:
x = x + 1
if the class x does not have the += method defined on it.
Similar:
counts("a") += 1
desugars:
counts("a") = counts("a") + 1
because counts("a") is Int , and Int does not have a specific += method.
On the other hand, the entry:
x(expression1) = expression2
desugars to call the update method in Scala:
x.update(expression1, expression2)
Each changed Map has an update method - it allows you to set keys on the map.
Thus, the entire expression is desugared for:
list.foreach(x => counts.update(x, counts(x) + 1))
This += should not be confused with the += method on mutable.Map in Scala. This method updates the record on the map if this key already exists, or adds a new key-value pair. It returns the this link, that is, the same map, so you can link += calls. See ScalaDoc or source code .
source share