As everyone else said, immutable Map
has no +=
method. Just want to add that maybe you confused it with compiler's shortcut for var
s:
scala> var a = Map(1 -> 1)
a: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1)
scala> a += 2 -> 2
scala> a
res2: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2)
This +=
shortcut is the same as a = a + 2 -> 2
, so it won't work for immutable values, like input parameters:
scala> def getMutated(m: Map[Int, Int]) = {m = m + (1 -> 1)}
<console>:7: error: reassignment to val
def getMutated(m: Map[Int, Int]) = {m = m + (1 -> 1)}
^
You could declare a var
inside your function, however the best and simplest way is to use +
as someone else said.