Scala 中的运算符优先级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2922347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Operator precedence in Scala
提问by Jeriho
I like Scala's propose of operator precedence but in some rare cases, unmodified rules may be inconvenient, because you have restrictions in naming your methods. Are there ways to define another rules for a class/file, etc. in Scala? If not, would it be resolved in the future?
我喜欢 Scala 的运算符优先级建议,但在极少数情况下,未修改的规则可能不方便,因为您在命名方法时有限制。有没有办法在 Scala 中为类/文件等定义另一个规则?如果没有,将来会解决吗?
回答by Thomas Jung
Operator precedence is fixed in the Scala Reference - 6.12.3 Infix Operationsby the first character in the operator. Listed in increasing order of precedence:
运算符优先级在Scala 参考 - 6.12.3 中缀操作中由运算符中的第一个字符固定。按优先级递增的顺序列出:
(all letters)
|
^
&
= !
< >
:
+ -
* / %
(all other special characters)
And it's not very probable that it will change. It will probably create more problems than it fixes. If you're used the normal operator precedence changing it for one class will be quite confusing.
而且改变的可能性不大。它可能会产生比它修复的更多的问题。如果您使用正常的运算符优先级,为一个类更改它会非常混乱。
回答by Randall Schulz
Are there ways to define another rules for a class/file, etc. in Scala? If not, would it be resolved in the future?
有没有办法在 Scala 中为类/文件等定义另一个规则?如果没有,将来会解决吗?
There is no such ability and there is little likelihood of it being added in the forseeable future.
没有这种能力,在可预见的未来加入的可能性很小。
回答by Luciano
There was a feature requestraised in the typelevel fork of the scala compiler, a version of the compiler which 'previews' experimental features. The developers suggested that if somebody were to write a SIPfor this, it may be considered for implementation.
在 Scala 编译器的typelevel fork 中提出了一个功能请求,这是一个“预览”实验性功能的编译器版本。开发人员建议,如果有人为此编写SIP,可以考虑实施。
But in it's current state, there is no way to override precedence. It's rules are formally defined in the language specification.
但在当前状态下,无法覆盖优先级。它的规则在语言规范中正式定义。
回答by Fuad Efendi
unmodified rules may be inconvenient, because you have restrictions in naming your methods
未修改的规则可能不方便,因为您在命名方法时有限制
- you do not have any restrictions in naming your methods. For example, you can define methods +, -, * and etc. for a class.
- we must also follow de-facto "unmodified rules" (enforced by Scala operator precedence rules) mentioned in previous answer (https://stackoverflow.com/a/2922456) by Thomas Jung - it is common for many if not all programming languages, and abstract algebra; we need not redefine operator precedence for a+b*c.
- 您在命名方法时没有任何限制。例如,您可以为类定义 +、-、* 等方法。
- 我们还必须遵循Thomas Jung在之前的回答 ( https://stackoverflow.com/a/2922456) 中提到的事实上的“未修改规则”(由 Scala 运算符优先规则强制执行)——这对于许多(如果不是所有)编程语言都很常见,和抽象代数;我们不需要为 a+b*c 重新定义运算符优先级。
See Chapter 6 of the book http://www.scala-lang.org/docu/files/ScalaByExample.pdffor "Rational" class example.
有关“Rational”类示例,请参阅本书http://www.scala-lang.org/docu/files/ScalaByExample.pdf 的第 6 章。

