Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 820
Dan Burton Avatar asked Oct 12 '11 17:10

Dan Burton


3 Answers

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

like image 111
missingfaktor Avatar answered Nov 12 '22 12:11

missingfaktor


Isn't it unambiguous, since I am not using m.+= ?

No, it isn't, because parenthesis can always be used when there are multiple arguments. For example:

List(1, 2, 3) mkString ("<", ", ", ">")

So you might ask, what multiple parameters? Well, the Scala API doc is your friend (or mine, at least), so I present you with:

scala> val m = scala.collection.mutable.Map[String, Int]()
m: scala.collection.mutable.Map[String,Int] = Map()

scala> m += (("foo", 2), ("bar", 3))
res0: m.type = Map(bar -> 3, foo -> 2)

In other words, += takes a vararg.

like image 21
Daniel C. Sobral Avatar answered Nov 12 '22 11:11

Daniel C. Sobral


The preferred way to define map entries is to use the -> method. Like

m += ("foo" -> 2)

Which constructs a tuple. a -> b gets desugared to a.->(b). Every object in scala has a -> method.

like image 3
Sean McCauliff Avatar answered Nov 12 '22 13:11

Sean McCauliff