3
使用斯卡拉2.11.8,我可以追加key - value
对通过一个地图:将Tuple添加到地图?
scala> Map((1 -> 1)) + (2 -> 2)
res8: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2)
但我怎么可以使用元组作为+
的说法?
scala> Map((1 -> 1)) + (2, 2)
<console>:12: error: type mismatch;
found : Int(2)
required: (Int, ?)
Map((1 -> 1)) + (2, 2)
^
也不这项工作:
scala> Map((1, 1)) + (2, 2)
<console>:12: error: type mismatch;
found : Int(2)
required: (Int, ?)
Map((1, 1)) + (2, 2)
^
<console>:12: error: type mismatch;
found : Int(2)
required: (Int, ?)
Map((1, 1)) + (2, 2)
^
Map#+的签名是:
+(kv: (A, B)): Map[A, B]
,所以我不知道为什么它不工作。
编辑
scala> Map((1,1)).+((2, 2))
res0: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2)
作品,但为什么不上?
'地图((1 - > 1))+((2,2))' – Jesper
于是两个括号'((2,2))'由于第一需要的是通过参数传递给'( ';第二个'(''开始元组? –
这是正确的。 – Iadams