Scala - 中缀与点符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10233227/
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
Scala - infix vs dot notation
提问by Kevin Li
Is there a best practice for one over the other? I've been reading theScala book by Odersky, et al. and it seems like infix is used for a lot of the Collections API functions, whereas dot is reserved for programmer-defined functions.
两者之间是否有最佳实践?我一直在阅读Odersky 等人的Scala 书。似乎中缀用于许多集合 API 函数,而 dot 保留用于程序员定义的函数。
回答by missingfaktor
I personally do not have any hard and fast rules for this, but I tend to use infix notation only with symbolic method names, and dot notation for alphanumeric ones.
我个人对此没有任何硬性规定,但我倾向于仅对符号方法名称使用中缀表示法,对字母数字方法名称使用点表示法。
Infix notation makes it cumbersome to modify code later. Here are some examples.
中缀表示法使得以后修改代码变得很麻烦。这里有些例子。
Imagine you have this line of code:
想象一下你有这行代码:
xs filter { f } map { g }
Suppose at some latter point in time you need to add a toListat end. You put it so:
假设在后面的某个时间点,您需要toList在末尾添加一个。你是这样说的:
xs filter { f } map { g } toList
This may cause semicolon inference issues. To avoid these issues, you either put a semicolon at end, or put a new line. Both options are ugly, in my opinion. To avoid all this nonsense, I prefer to go with xs.filter(f).map(g). It's always easier to refactor with this syntax.
这可能会导致分号推断问题。为避免这些问题,您要么在末尾添加分号,要么换行。在我看来,这两种选择都很丑陋。为了避免所有这些废话,我更喜欢使用xs.filter(f).map(g). 使用这种语法重构总是更容易。
Another example: Say I have the following in my code:
另一个例子:假设我的代码中有以下内容:
if(foo contains bar) { ..
Say, I need to negate the condition. If I modify it as follows:
说,我需要否定条件。如果我修改如下:
if(!foo contains bar) { ..
Bummer. This gets parsed as (!foo).contains(bar). Not what we wanted.
无赖。这被解析为(!foo).contains(bar). 不是我们想要的。
Or suppose you need to add a new condition in addition, and you modify it so:
或者假设您需要另外添加一个新条件,然后修改它:
if(foo contains bar && cond) { ..
Another bummer. This gets parsed as foo.contains(bar.&&(cond)). Not what we wanted, again.
另一个无赖。这被解析为foo.contains(bar.&&(cond)). 又不是我们想要的。
Of course, you could add a bunch of parentheses around, but that would be ugly and hard to read/edit as compared with dot notation.
当然,您可以在周围添加一堆括号,但与点表示法相比,这会很丑陋且难以阅读/编辑。
Now, all of what I said above applies to symbolic method names too. However symbolic methods look unnatural when used with dot syntax, and so I prefer the infix syntax for them.
现在,我上面所说的所有内容也适用于符号方法名称。然而,符号方法在与点语法一起使用时看起来不自然,因此我更喜欢它们的中缀语法。
One exception to the guideline above: Internal DSLs. They are usually crafted with care so as not to cause parsing issues when written in the manner prescribed in their documentation/examples (which usually uses infix notation).
上述指南的一个例外:内部 DSL。它们通常是精心制作的,以免在以文档/示例中规定的方式编写时引起解析问题(通常使用中缀表示法)。
回答by dhg
It's a matter of personal preference. Your decision to use one style or the other should be based on what makes your code the most readable.
这是个人喜好的问题。您决定使用一种风格还是另一种风格应该基于什么使您的代码最具可读性。
But note that the ability to leave off the dot and parentheses is limited only to certain syntactic constructions, so sometimes you just have to fall back to using them.
但请注意,省略点和括号的能力仅限于某些句法结构,因此有时您必须回退到使用它们。
回答by Saeed Zarinfam
There is a good style guidein official Scala site documentation that describe proper usage infix notation over dot notation.
在官方 Scala 站点文档中有一个很好的样式指南,它描述了正确使用中缀表示法而不是点表示法。
Suffix Notation:
后缀符号:
names.toList
// is the same as
names toList // Unsafe, don't use!
Arity-1:
Arity-1:
// right!
names foreach (n => println(n))
names mkString ","
optStr getOrElse "<empty>"
// wrong!
javaList add item
Higher-Order Functions:
高阶函数:
// wrong!
names.map (_.toUpperCase).filter (_.length > 5)
// right!
names map (_.toUpperCase) filter (_.length > 5)
Symbolic methods/Operators:
符号方法/运算符:
// right!
"daniel" + " " + "Spiewak"
// wrong!
"daniel"+" "+"spiewak"
回答by Peter Perhá?
I have found that using infix notation for mapworks nicely when I am creating cartesians with the catslibrary. e.g.:
我发现,使用中缀表示法的map作品很好,当我创建一个具有笛卡儿猫库。例如:
(fetchIt(1) |@| fetchIt(2) |@| fetchIt(3)).map(MyCaseClass)
you can get rid of the surrounding parentheses like so:
你可以像这样去掉周围的括号:
fetchIt(1) |@| fetchIt(2) |@| fetchIf(3) map MyCaseClass
and in my view the second variant reads nicer. A matter of taste I suppose. Just wanted to add my two cents' worth.
在我看来,第二个变体读起来更好。我想是口味问题。只是想增加我的两分钱。
The above works because |in "|@|" has higher precedence than min "map". Read this part of Scala lang specification to find out more detail:
上述工作因为|在“|@|” 具有比m“map”更高的优先级。阅读 Scala lang 规范的这一部分以了解更多细节:
If there are several infix operations in an expression, then operators with higher precedence bind more closely than operators with lower precedence.
如果表达式中有多个中缀操作,则优先级较高的运算符比优先级较低的运算符绑定得更紧密。
http://scala-lang.org/files/archive/spec/2.12/06-expressions.html#infix-operations
http://scala-lang.org/files/archive/spec/2.12/06-expressions.html#infix-operations

