@@ -54,19 +54,19 @@ readability and will make it much easier to understand at a glance the
54
54
most basic operation of any given method. Resist the urge to omit
55
55
parentheses simply to save two characters!
56
56
57
- ### Suffix Notation
57
+ ## Postfix Notation
58
58
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:
60
60
61
+ // recommended
61
62
names.toList
62
63
63
- // is the same as
64
-
65
- names toList // Unsafe, don't use!
64
+ // discourage
65
+ names toList
66
66
67
67
This style is unsafe, and should not be used. Since semicolons are
68
68
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.
70
70
71
71
names toList
72
72
val answer = 42 // will not compile!
@@ -75,83 +75,44 @@ This may result in unexpected compile errors at best, and happily
75
75
compiled faulty code at worst. Although the syntax is used by some
76
76
DSLs, it should be considered deprecated, and avoided.
77
77
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.
103
80
104
- ### Higher-Order Functions
81
+ ## Infix notation
105
82
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:
109
86
110
- names.map (_.toUpperCase) // wrong!
87
+ // recommended
88
+ a + b
111
89
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
115
92
116
- // wrong!
117
- names.map (_.toUpperCase).filter (_.length > 5 )
93
+ // legal, but definitely strange
94
+ a.+(b )
118
95
119
- // right!
120
- names map (_.toUpperCase) filter (_.length > 5)
96
+ but avoid it for almost all alphabetic-named methods:
121
97
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(",")
136
100
137
- // right!
138
- "daniel" + " " + "Spiewak "
101
+ // also sometimes seen; controversial
102
+ names mkString ", "
139
103
140
- // wrong!
141
- "daniel"+" "+"spiewak"
104
+ A gray area is short, operator-like methods like ` max ` ,
105
+ especially if commutative:
142
106
143
- For the most part, this idiom follows Java and Haskell syntactic
144
- conventions.
107
+ // fairly common
108
+ a max b
145
109
146
110
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:
148
112
149
113
foo ** (bar, baz)
150
114
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