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.
相关问题
- Unusual use of the new keyword
- What uses more memory in c++? An 2 ints or 2 funct
- How to get the maximum of more than 2 numbers in V
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- Capture method calls in Java
- GRPC: make high-throughput client in Java/Scala
- Setting up multiple test folders in a SBT project
- Testing request with CSRF Token in Play framework
There is a good style guide in official Scala site documentation that describe proper usage infix notation over dot notation.
Suffix Notation:
Arity-1:
Higher-Order Functions:
Symbolic methods/Operators:
I have found that using infix notation for
map
works nicely when I am creating cartesians with the cats library. e.g.:you can get rid of the surrounding parentheses like so:
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 thanm
in "map". Read this part of Scala lang specification to find out more detail:http://scala-lang.org/files/archive/spec/2.12/06-expressions.html#infix-operations
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:
Suppose at some latter point in time you need to add a
toList
at end. You put it so: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:
Say, I need to negate the condition. If I modify it as follows:
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:
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.