斯卡拉(Scala):为什么我必须在这里加括号? [英] Scala: why do I have to put extra parenthesis here?

查看:71
本文介绍了斯卡拉(Scala):为什么我必须在这里加括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的需要围绕最后一个元组的圆括号吗?没有它们,它将无法编译,并且编译器将尝试仅添加Sort( time)并抱怨它期望使用元组。

Are the parenthesis around the final tuple really needed? It doesn't compile without them and the compiler tries to add only the Sort("time") and complains that it expects a tuple instead.

val maxSortCounts: Map[Sort, Int] =
  sorts.map(s => s -> usedPredicates.map(pred => pred.signature.count(_ == s)).max)
    .toMap + ((Sort("time"), 1))

我尝试用一​​个简短的示例在REPL内重现此行为,但在此行为符合预期。变量 sorts Seq [Sort]

I've tried to reproduce this behaviour inside the REPL with a shorter example, but there it behaves as intended. The variable sorts is a Seq[Sort].

error: type mismatch;
found   : <snip>.Sort
required: (<snip>.Sort, Int)
.toMap + (Sort("time"), 1)


推荐答案

是的,它们是必需的。否则,编译器会将代码解释为
x。+(y,z)而不是 x。+((y,z) )

Yes, they are needed. Otherwise the compiler will interpret the code as x.+(y, z) instead of x.+((y, z)).

相反,您可以再次使用ArrowAssoc: x +(y-> z)。注意,也需要括号,因为 + -具有相同的优先级(仅方法的第一个符号定义其优先级)。

Instead, you can use ArrowAssoc again: x + (y -> z). Notice, the parentheses are also needed because + and - have the same precedence (only the first sign of a method defines its precedence).

这篇关于斯卡拉(Scala):为什么我必须在这里加括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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