4

As the code shows:

val map = scala.collection.mutable.Map[Int, Int]().withDefaultValue(0) println(map(1)) map(1) = 10 println(map(1)) map(1) += 10 println(map(1)) 

and the output:

0 10 20 

However, in my opinion, "map(1) += 10" is invalid as in java, and even valid, the result after this line, map(1) should be 10.

So Why??? and what exactly operation "+=" do in map ??

2

3 Answers 3

6

First, += is an assignment operator, because it ends in = and isn't a comparison operator. This means that if there is no suitable method (and Int doesn't have a += method), map(1) += 10 is converted to map(1) = map(1) + 10. By the rules in Assignments, this becomes map.update(1, map(1) + 10) and this becomes map.update(1, map.apply(1) + 10) because map is an object rather than a method.

Sign up to request clarification or add additional context in comments.

Comments

3

The question asked 'what exactly operation "+=" do in map ??' is not relevant to the situation you described. Map's method += adds entry (key -> value) to Map, so that map(key) == value.

So the question is: what does map(1) += 10 does.

This simple lines

 map(1) += 10 

compiles into this javacode

 24: invokestatic #36 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; 27: aload_2 28: iconst_1 29: invokestatic #36 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; 32: invokeinterface #43, 2 // InterfaceMethod scala/collection/mutable/Map.apply:(Ljava/lang/Object;)Ljava/lang/Object; 37: invokestatic #47 // Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I 40: bipush 10 42: iadd 43: invokestatic #36 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; 46: invokeinterface #51, 3 // InterfaceMethod scala/collection/mutable/Map.update:(Ljava/lang/Object;Ljava/lang/Object;)V 

Remove boxing and unboxing ops - we are not interested in them(line 24, 29, 37, 43). Review the rest:

We get value that was in map

 27: aload_2 28: iconst_1 32: invokeinterface #43, 2 // InterfaceMethod scala/collection/mutable/Map.apply:(Ljava/lang/Object;)Ljava/lang/Object; 

Add 10 to it:

 40: bipush 10 42: iadd 

And accomplish update:

 46: invokeinterface #51, 3 // InterfaceMethod scala/collection/mutable/Map.update:(Ljava/lang/Object;Ljava/lang/Object;)V 

Seems map(1) += 10 is desugared to map.update(1, map.apply(1) + 10)

Comments

0

In Scala Map. += means

ms += (k -> v) Adds mapping from key k to value v to map ms as a side effect and returns ms itself. ms += (k -> v, l -> w) Adds the given mappings to ms as a side effect and returns ms itself. 

2 Comments

On mutable map it's syntatic sugar for apply + update or in this case map.update(1, map.apply(1) + 10)
But in this case it isn't called on the map, but on map(1).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.