Scala Map,元组和函数参数列表之间的歧义 [英] Scala Map, ambiguity between tuple and function argument list

查看:81
本文介绍了Scala Map,元组和函数参数列表之间的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)不起作用怎么办? Scala给出了类型错误:

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)
             ^

显然,Scala认为我试图使用两个参数而不是一个元组参数来调用+=.为什么?因为我没有使用m.+=吗?

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, ..)降低了对a.b(c, d, e, ..)的吸引力.因此是错误.

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

这篇关于Scala Map,元组和函数参数列表之间的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆