English 中文(简体)
图表、图和功能理由清单之间的模糊
原标题: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

因此,如何处理<代码>m +=(“foo”),2)没有工作? 胎面错误:

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

最佳回答

遗憾的是,a b (c, d, e, .) desugars to a.b(c, d, e, .)。 因此错误。

问题回答

鉴于我没有使用m.+=?

无,因为有多重论点时总是可以使用母体。 例如:

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

因此,请多个参数? 顺便提一下,我向各位介绍:

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)

换言之,+= 采取灵活做法。

界定地图条目的首选方法是使用“->”方法。 和

m += ("foo" -> 2)

什么是构造的。 a-> b 接触到a.->(b)。 每个 object体内的物体都有一种――和法。





相关问题
Which name for a "smart" dictionary (hashtable)?

I m looking for a good name for a custom dictionary which automatically initializes the value for a requested key if it doesn t exist, using a delegate. The indexer implementation should help ...

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Python dictionary simple way to add a new key value pair

Say you have, foo = bar d = { a-key : a-value } And you want d = { a-key : a-value , foo : bar } e = { foo :foo} I know you can do, d[ foo ] = foo #Either of the following for e e = { foo :foo}...

Storing ints in a Dictionary

As I understand, in Objective-C you can only put Objects into dictionaries. So if I was to create a dictionary, it would have to have all objects. This means I need to put my ints in as NSNumber, ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签