You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _style/naming-conventions.md
+25-33
Original file line number
Diff line number
Diff line change
@@ -195,44 +195,36 @@ getter and setter. For example:
195
195
196
196
### Parentheses
197
197
198
-
Unlike Ruby, Scala attaches significance to whether or not a method is
199
-
*declared* with parentheses (only applicable to methods of
200
-
[arity](https://en.wikipedia.org/wiki/Arity)-0). For example:
198
+
Scala allows a parameterless, zero-[arity](https://en.wikipedia.org/wiki/Arity)
199
+
method to be declared with an empty parameter list:
201
200
202
201
def foo1() = ...
203
202
203
+
or with no parameter lists at all:
204
+
204
205
def foo2 = ...
205
206
206
-
These are different methods at compile-time. While `foo1` can be called
207
-
with or without the parentheses, `foo2`*may not* be called *with*
208
-
parentheses.
209
-
210
-
Thus, it is actually quite important that proper guidelines be observed
211
-
regarding when it is appropriate to declare a method without parentheses
212
-
and when it is not.
213
-
214
-
Methods which act as accessors of any sort (either encapsulating a field
215
-
or a logical property) should be declared *without* parentheses except
216
-
if they have side effects. While Ruby and Lift use a `!` to indicate
217
-
this, the usage of parens is preferred (please note that fluid APIs and
218
-
internal domain-specific languages have a tendency to break the
219
-
guidelines given below for the sake of syntax. Such exceptions should
220
-
not be considered a violation so much as a time when these rules do not
221
-
apply. In a DSL, syntax should be paramount over convention).
222
-
223
-
Further, the callsite should follow the declaration; if declared with
224
-
parentheses, call with parentheses. While there is temptation to save a
225
-
few characters, if you follow this guideline, your code will be *much*
226
-
more readable and maintainable.
227
-
228
-
// doesn't change state, call as birthdate
229
-
def birthdate = firstName
230
-
231
-
// updates our internal state, call as age()
232
-
def age() = {
233
-
_age = updateAge(birthdate)
234
-
_age
235
-
}
207
+
By convention, parentheses are used to indicate that a method has
208
+
side effects.
209
+
210
+
On the other hand, the absence of parentheses indicates that a
211
+
method is like an accessor: it returns a value without altering the
212
+
receiver, and on the same receiver in the same state, it always
213
+
returns the same answer.
214
+
215
+
The callsite should follow the declaration; if declared with
216
+
parentheses, call with parentheses.
217
+
218
+
These conventions are followed in the Scala standard library and
219
+
you should follow them in your own code as well.
220
+
221
+
Additional notes:
222
+
223
+
* Scala 3 errors if you leave out the parentheses at the call site. Scala 2 merely warns.
224
+
* Scala 3 and 2 both error if the call site has parentheses where the definition doesn't.
225
+
* Java-defined methods are exempt from this distinction and may be called either way.
226
+
* If a method _does_ take parameters, there isn't any convention for indicating whether it also has side effects.
227
+
* Creating an object isn't considered a side effect. So for example, Scala collections have an `iterator` method with no parens. Yes, you get a new iterator each time. And yes, iterators are mutable. But every fresh iterator is the same until it has been altered by calling a side-effecting method such as `Iterator#next()`, which _is_ declared with parentheses. See this [2018 design discussion](https://github.com/scala/collection-strawman/issues/520).
0 commit comments