Scala - infix vs dot notation

感情迁移 提交于 2019-11-28 04:40:19

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 toList at end. You put it so:

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.

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.

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.

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

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.

There is a good style guide in official Scala site documentation that describe proper usage infix notation over dot notation.

Suffix Notation:

names.toList
// is the same as
names toList // Unsafe, don't use!

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"

I have found that using infix notation for map works nicely when I am creating cartesians with the cats library. e.g.:

(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 m in "map". Read this part of Scala lang specification to find out more detail:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!