将值附加到MAP时,为什么Scala需要额外的括号块才能使此语句有效?
不编译:
vmap += (item.getName(), item.getString()) // compiler output: "found: String"
Run Code Online (Sandbox Code Playgroud)
但是,这会编译:
vmap += ((item.getName(), item.getString())) // note the second set of enclosures
Run Code Online (Sandbox Code Playgroud)
TIA
编辑:vmap定义为
val vmap = new mutable.HashMap[String, String]()
Run Code Online (Sandbox Code Playgroud)
结语:
在编辑时,有一些帖子详细说明了两种可能的解释,这两种解释似乎都含有真实的元素.哪一个实际上是正确的?我无法肯定地说...我只是一个仍在学习语言的人.话虽这么说,我已经根据一个答案(至少在某种程度上)包含在另一个答案中的感觉改变了答案选择- 所以我选择了更大的图片,因为我认为它会提供更广泛的意义为其他人寻找答案.具有讽刺意味的是,我试图更好地理解如何消除语言的一些细微差别,而我已经意识到的是,有更多的东西比我怀疑的更多.我不是说这是一件坏事 - 事实上(IMO)可以从任何语言中获得灵活和复杂的预期- 但它肯定会让一个人不时地错过黑白组织的世界...
为了得出结论,一些观察结果:
1)所选答案包含一个链接到一个充满Scala脑弯曲的网站(我发现这对于尝试理解语言中的一些上述夸克非常有帮助. ) 强烈推荐.
2)我确实遇到了另一个有趣的转折 - 而单括号(上面的例子)不起作用,改变它如下,它工作得很好......
vmap += ("foo" -> "bar")
Run Code Online (Sandbox Code Playgroud)
这可能与匹配方法/功能签名有关,但这只是我的一个猜测.
接受的答案实际上是错误的.
你没有得到Map.+ =的元组的原因是该方法被第二个带有两个或更多args的方法重载.
如果args的数量错误,编译器将只尝试tupling.但是如果你给它两个args,并且有一个方法需要两个,这就是它所选择的,即使它没有键入check.
它没有开始尝试所有可能的组合,直到有效的方式,因为这将充满默默无闻.(Cf暗示.)
scala> def f(p: Pair[String, Int]) = { val (s,i) = p; s.toInt + i }
f: (p: Pair[String,Int])Int
scala> f("2",3) // ok to tuple
res0: Int = 5
scala> trait F { def +=(p: Pair[String, Int]) = f(p) }
defined trait F
scala> val foo = new F {}
foo: F = $anon$1@6bc77f62
scala> foo += ("2",3) // ok to tuple
res1: Int = 5
scala> trait G { def +=(p: Pair[String, Int]) = f(p); def +=(p:(String,Int),q:(String,Int),r:(String,Int)*) = f(p)+f(q)+(r map f).sum }
defined trait G
scala> val goo = new G {}
goo: G = $anon$1@183aeac3
scala> goo += ("2",3) // sorry
<console>:12: error: type mismatch;
found : String("2")
required: (String, Int)
goo += ("2",3)
^
scala> goo += (("2",3),("4",5),("6",7))
res3: Int = 27
Run Code Online (Sandbox Code Playgroud)
我不能不提你的朋友和我的,-Xlint,它会警告不适当的arg改编:
apm@mara:~/tmp$ skala -Xlint
Welcome to Scala version 2.11.0-20130811-132927-95a4d6e987 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def f(p: Pair[String, Int]) = { val (s,i) = p; s.toInt + i }
f: (p: Pair[String,Int])Int
scala> f("2",3)
<console>:9: warning: Adapting argument list by creating a 2-tuple: this may not be what you want.
signature: f(p: Pair[String,Int]): Int
given arguments: "2", 3
after adaptation: f(("2", 3): (String, Int))
f("2",3)
^
res0: Int = 5
scala> List(1).toSet()
<console>:8: warning: Adapting argument list by inserting (): this is unlikely to be what you want.
signature: GenSetLike.apply(elem: A): Boolean
given arguments: <none>
after adaptation: GenSetLike((): Unit)
List(1).toSet()
^
res3: Boolean = false
Run Code Online (Sandbox Code Playgroud)
关于适应的危险,请参阅自适应推理益智游戏和这个非常常见的新游戏因为我们了解到parens的存在与否在很大程度上是一种风格问题,当parens真正重要时,使用它们会导致类型错误.
在存在重载的情况下调整元组:
scala> class Foo {
| def f[A](a: A) = 1 // A can be (Int,Int,Int)
| def f[A](a: A, a2: A) = 2
| }
defined class Foo
scala> val foo = new Foo
foo: Foo = Foo@2645d22d
scala> foo.f(0,0,0)
<console>:10: warning: Adapting argument list by creating a 3-tuple: this may not be what you want.
signature: Foo.f[A](a: A): Int
given arguments: 0, 0, 0
after adaptation: Foo.f((0, 0, 0): (Int, Int, Int))
foo.f(0,0,0)
^
res9: Int = 1
Run Code Online (Sandbox Code Playgroud)