I was writing some code today when I noticed this quirky behavior. It appears that immutable maps nested within mutable maps allow for the (usually mutable) += operator.
scala> val myMutableMap = mutable.Map[String, scala.collection.immutable.Map[String, String]]() myMutableMap: scala.collection.mutable.Map[String,scala.collection.immutable.Map[String,String]] = Map() scala> myMutableMap += "outerkey" -> scala.collection.immutable.Map("k1"-> "v1") res25: myMutableMap.type = Map(outerkey -> Map(k1 -> v1)) scala> myMutableMap("outerkey") += "k2"->"v2" scala> myMutableMap res27: scala.collection.mutable.Map[String,scala.collection.immutable.Map[String,String]] = Map(outerkey -> Map(k1 -> v1, k2 -> v2)) scala> val huhwhat = myMutableMap("outerkey") huhwhat: scala.collection.immutable.Map[String,String] = Map(k1 -> v1, k2 -> v2) scala> huhwhat += "k3"->"k4" <console>:21: error: value += is not a member of scala.collection.immutable.Map[String,String] huhwhat += "k3"->"k4" I looked into the Map.scala source, but didn't see any obvious answers to where the += operator could be inherited from.
This is on Scala 2.11.5. Does anyone know what's going on?