Dan*_*ton 7 scala tuples function map infix-notation
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
Run Code Online (Sandbox Code Playgroud)
那么m += ("foo" , 2)没有工作的协议是什么?Scala给出了类型错误:
error: type mismatch;
found : java.lang.String("foo")
required: (String, Int)
m += ("foo", 2)
^
Run Code Online (Sandbox Code Playgroud)
显然Scala认为我试图+=用两个参数调用,而不是一个元组参数.为什么?这不是明确的,因为我没有使用m.+=?
不幸的是a b (c, d, e, ..)去世了a.b(c, d, e, ..).因此错误.