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: _overviews/scala3-book/methods-most.md
+21-13Lines changed: 21 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ This section introduces the various aspects of how to define and call methods in
13
13
14
14
Scala methods have many features, including these:
15
15
16
-
- Generic (type) parameters
16
+
- Generic methods with type parameters
17
17
- Default parameter values
18
18
- Multiple parameter groups
19
19
- Context-provided parameters
@@ -197,14 +197,13 @@ engage(
197
197
198
198
199
199
200
-
## A suggestion about methods that take no parameters
200
+
## A convention about methods that take no parameters
201
201
202
-
When a method takes no parameters, it’s said to have an _arity_ level of _arity-0_.
203
-
Similarly, when a method takes one parameter it’s an _arity-1_ method.
202
+
When a method takes no parameters, it’s said to have an _arity_ level of _arity-0_. Similarly, when a method takes one parameter it’s an _arity-1_ method.
204
203
When you create arity-0 methods:
205
204
206
205
- If the method performs side effects, such as calling `println`, declare the method with empty parentheses
207
-
- If the method does not perform side effects---such as getting the size of a collection, which is similar to accessing a field on the collection---leave the parentheses off
206
+
- If the method does not perform side effects---about such methods they say "pure methods" or "pure code", contrary to "dirty" or "impure"---leave the parentheses off
208
207
209
208
For example, this method performs a side effect, so it’s declared with empty parentheses:
210
209
@@ -219,7 +218,17 @@ speak // error: "method speak must be called with () argument"
219
218
speak() // prints "hi"
220
219
```
221
220
222
-
While this is just a convention, following it dramatically improves code readability: It makes it easier to understand at a glance that an arity-0 method performs side effects.
221
+
The main intention behind this convention is to make transition field-to-method and visa-versa easy for developers and transparent to method consumers. For example, given the following code, no one can tell if `speak` is a field or method:
222
+
223
+
```scala
224
+
valalleyCat=Cat("Oliver")
225
+
println(alleyCat.speak)
226
+
```
227
+
228
+
In FP terms "call of pure function" and "result of pure function" (with given arguments) are totally the same thing. While code does not change any existing state we can substitute field instead of method, and method instead of the field. The result must be the same.
229
+
230
+
However, in the case of impure code consumers must be warned and alarmed at least, because impure code tends to make dangerous things, like throwing `IOException`. That is why there is a difference between an absent list and an empty argument list.
231
+
223
232
224
233
{% comment %}
225
234
Some of that wording comes from this page: https://docs.scala-lang.org/style/method-invocation.html
@@ -269,7 +278,7 @@ For more details on the `Matchable` trait, see the [Reference documentation][ref
269
278
270
279
## Controlling visibility in classes
271
280
272
-
In classes, objects, traits, and enums, Scala methods are public by default, so the `Dog` instance created here can access the `speak` method:
281
+
In classes, objects, traits, and enums, Scala methods are public by default, so world can access the `speak` method of `Dog` instance created here:
273
282
274
283
```scala
275
284
classDog:
@@ -279,16 +288,15 @@ val d = new Dog
279
288
d.speak() // prints "Woof"
280
289
```
281
290
282
-
Methods can also be marked as `private`.
283
-
This makes them private to the current class, so they can’t be called nor overridden in subclasses:
291
+
Methods can also be marked as `private`. This makes them private to the current class, so they can’t be called nor overridden in subclasses:
If you want to make a method private to the current class and also allow subclasses to call it or override it, mark the method as `protected`, as shown with the `speak` method in this example:
@@ -299,10 +307,10 @@ class Animal:
299
307
defwalk() =
300
308
breathe()
301
309
println("I’m walking")
302
-
protecteddefspeak() = println("Hello?")
310
+
protecteddefspeak() = println("Hi people")
303
311
304
312
classCatextendsAnimal:
305
-
overridedefspeak() = println("Meow")
313
+
overridedefspeak() = println("Meow people")
306
314
307
315
valcat=newCat
308
316
cat.walk()
@@ -321,7 +329,7 @@ The `protected` setting means:
321
329
322
330
Earlier you saw that traits and classes can have methods.
323
331
The Scala `object` keyword is used to create a singleton class, and an object can also contain methods.
324
-
This is a nice way to group a set of “utility” methods.
332
+
This is a usual way to group a set of “utility” methods.
325
333
For instance, this object contains a collection of methods that work on strings:
0 commit comments