val m = scala.collection.mutable.Map[String, Int]()
// this doesn't work
m += ("foo", 2)
// this does work
m += (("foo", 2))
// this works too
val barpair = ("bar", 3)
m += barpair
So what's the deal with m += ("foo" , 2)
not working? Scala gives the type error:
error: type mismatch;
found : java.lang.String("foo")
required: (String, Int)
m += ("foo", 2)
^
Apparently Scala thinks that I am trying to call +=
with two arguments, instead of one tuple argument. Why? Isn't it unambiguous, since I am not using m.+=
?
m += ("foo" -> 2)
or evenm += "foo" -> 2
. – Heimlich