Skip to content

Commit a3732aa

Browse files
committed
style guide: revise sections on infix and postfix notation
the postfix text is mostly the same, but now uses the word "postfix" instead of "suffix" the infix text is totally rewritten and now more strongly discourages use of infix for alphabetic method names. back when the style guide was first written, I think there was more experimentation with, and more acceptance of, such use of infix notation. these days, not so much. I omitted the material about mixing dot and dotless notation -- the "a b.(c) d" example. I found that passage highly confusing and I've seen others be confused by it as well.
1 parent 8a75768 commit a3732aa

File tree

1 file changed

+32
-71
lines changed

1 file changed

+32
-71
lines changed

style/method-invocation.md

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ readability and will make it much easier to understand at a glance the
5454
most basic operation of any given method. Resist the urge to omit
5555
parentheses simply to save two characters!
5656

57-
### Suffix Notation
57+
## Postfix Notation
5858

59-
Scala allows methods of arity-0 to be invoked using suffix notation:
59+
Scala allows methods that take no arguments to be invoked using postfix notation:
6060

61+
// recommended
6162
names.toList
6263

63-
// is the same as
64-
65-
names toList // Unsafe, don't use!
64+
// discourage
65+
names toList
6666

6767
This style is unsafe, and should not be used. Since semicolons are
6868
optional, the compiler will attempt to treat it as an infix method
69-
if it can, potentially taking a term from the next line.
69+
if it can, potentially taking a term from the next line.
7070

7171
names toList
7272
val answer = 42 // will not compile!
@@ -75,83 +75,44 @@ This may result in unexpected compile errors at best, and happily
7575
compiled faulty code at worst. Although the syntax is used by some
7676
DSLs, it should be considered deprecated, and avoided.
7777

78-
As of Scala 2.10, using suffix operator notation will result in a compiler warning.
79-
80-
## Arity-1
81-
82-
Scala has a special syntax for invoking methods of arity-1 (one
83-
argument):
84-
85-
names.mkString(",")
86-
87-
// is the same as
88-
89-
names mkString ","
90-
91-
This syntax is formally known as "infix notation". It should *only* be
92-
used for purely-functional methods (methods with no side-effects) - such
93-
as `mkString` -or methods which take functions as parameters - such as
94-
`foreach`:
95-
96-
// right!
97-
names foreach (n => println(n))
98-
names mkString ","
99-
optStr getOrElse "<empty>"
100-
101-
// wrong!
102-
javaList add item
78+
Since Scala 2.10, using postfix operator notation will result in a
79+
compiler warning.
10380

104-
### Higher-Order Functions
81+
## Infix notation
10582

106-
As noted, methods which take functions as parameters (such as `map` or
107-
`foreach`) should be invoked using infix notation. It is also *possible*
108-
to invoke such methods in the following way:
83+
Scala has a special punctuation-free syntax for invoking methods that
84+
take one argument. Many Scala programmers use this notation for
85+
symbolic-named methods:
10986

110-
names.map (_.toUpperCase) // wrong!
87+
// recommended
88+
a + b
11189

112-
This style is *not* the accepted standard! The reason to avoid this
113-
style is for situations where more than one invocation must be chained
114-
together:
90+
// legal, but less readable
91+
a+b
11592

116-
// wrong!
117-
names.map (_.toUpperCase).filter (_.length > 5)
93+
// legal, but definitely strange
94+
a.+(b)
11895

119-
// right!
120-
names map (_.toUpperCase) filter (_.length > 5)
96+
but avoid it for almost all alphabetic-named methods:
12197

122-
Both of these work, but the former exploits an extremely unintuitive
123-
wrinkle in Scala's grammar. The sub-expression
124-
`(_.toUpperCase).filter` when taken in isolation looks for all the
125-
world like we are invoking the `filter` method on a function value.
126-
However, we are actually invoking `filter` on the result of the `map`
127-
method, which takes the function value as a parameter. This syntax is
128-
confusing and often discouraged in Ruby, but it is shunned outright in
129-
Scala.
130-
131-
## Symbolic methods/Operators
132-
133-
Methods with symbolic names should *always* be invoked using infix
134-
notation with spaces separating the target, the symbolic method and the
135-
parameter:
98+
// recommended
99+
names.mkString(",")
136100

137-
// right!
138-
"daniel" + " " + "Spiewak"
101+
// also sometimes seen; controversial
102+
names mkString ","
139103

140-
// wrong!
141-
"daniel"+" "+"spiewak"
104+
A gray area is short, operator-like methods like `max`,
105+
especially if commutative:
142106

143-
For the most part, this idiom follows Java and Haskell syntactic
144-
conventions.
107+
// fairly common
108+
a max b
145109

146110
Symbolic methods which take more than one parameter (they do exist!)
147-
should still be invoked using infix notation, delimited by spaces:
111+
may still be invoked using infix notation, delimited by spaces:
148112

149113
foo ** (bar, baz)
150114

151-
Such methods are fairly rare, however, and should be avoided during API
152-
design.
153-
154-
Finally, the use of the `/:` and `:\` should be avoided in preference to
155-
the more explicit `foldLeft` and `foldRight` method of `Iterator`. The
156-
right-associativity of the `/:` can lead to extremely confusing code, at
157-
the benefit of saving a few characters.
115+
Such methods are fairly rare, however, and should normally be avoided
116+
during API design. For example, the use of the `/:` and `:\` methods
117+
should be avoided in preference to their better-known names,
118+
`foldLeft` and `foldRight`.

0 commit comments

Comments
 (0)