Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - infix vs dot notation

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.

like image 277
Kevin Li Avatar asked Apr 19 '12 17:04

Kevin Li


3 Answers

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

like image 106
missingfaktor Avatar answered Nov 18 '22 18:11

missingfaktor


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.

like image 30
dhg Avatar answered Nov 18 '22 18:11

dhg


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"
like image 4
Saeed Zarinfam Avatar answered Nov 18 '22 19:11

Saeed Zarinfam