Search code examples
functionscaladictionarytuplesinfix-notation

Scala Map, ambiguity between tuple and function argument list


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.+= ?


Solution

  • Unfortunately a b (c, d, e, ..) desugars to a.b(c, d, e, ..). Hence the error.