pavelmk
pavelmk

Reputation: 53

Immutable maps within Mutable maps support Mutable operations

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?

Upvotes: 3

Views: 88

Answers (1)

nornagon
nornagon

Reputation: 15811

myMutableMap("outerkey") += "k2"->"v2"

is being interpreted by the compiler as

myMutableMap("outerkey") = myMutableMap("outerkey") + ("k2"->"v2")

i.e., create a new map from the immutable myMutableMap("outerkey") with the added key ("k2"->"v2"), and update myMutableMap with the new pair ("outerkey" -> <the new map>).

This is implemented by mutable.MapLike's update method, which provides the foo(x) = y behaviour. See this blog post for more info (and there's also an SO answer listing a bunch of other magic functions in Scala).

Upvotes: 3

Related Questions