Scala - infix vs dot notation

2020-02-02 07:17发布

问题:

Is there a best practice for one over the other? I've been reading the Scala 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.

回答1:

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



回答2:

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.



回答3:

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"


回答4:

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