Skip to content

Commit 9182aa8

Browse files
committed
5 misphrasings and 1 applied theory
1 parent 426ab70 commit 9182aa8

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

_overviews/scala3-book/methods-most.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This section introduces the various aspects of how to define and call methods in
1313

1414
Scala methods have many features, including these:
1515

16-
- Generic (type) parameters
16+
- Generic methods with type parameters
1717
- Default parameter values
1818
- Multiple parameter groups
1919
- Context-provided parameters
@@ -197,14 +197,13 @@ engage(
197197

198198

199199

200-
## A suggestion about methods that take no parameters
200+
## A convention about methods that take no parameters
201201

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.
204203
When you create arity-0 methods:
205204

206205
- 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
208207

209208
For example, this method performs a side effect, so it’s declared with empty parentheses:
210209

@@ -219,7 +218,17 @@ speak // error: "method speak must be called with () argument"
219218
speak() // prints "hi"
220219
```
221220

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+
val alleyCat = 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+
223232

224233
{% comment %}
225234
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
269278

270279
## Controlling visibility in classes
271280

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:
273282

274283
```scala
275284
class Dog:
@@ -279,16 +288,15 @@ val d = new Dog
279288
d.speak() // prints "Woof"
280289
```
281290

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:
284292

285293
```scala
286294
class Animal:
287295
private def breathe() = println("I’m breathing")
288296

289297
class Cat extends Animal:
290298
// this method won’t compile
291-
override def breathe() = println("Yo, I’m totally breathing")
299+
override def breathe() = println("Meow. I’m breathing too.")
292300
```
293301

294302
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:
299307
def walk() =
300308
breathe()
301309
println("I’m walking")
302-
protected def speak() = println("Hello?")
310+
protected def speak() = println("Hi people")
303311

304312
class Cat extends Animal:
305-
override def speak() = println("Meow")
313+
override def speak() = println("Meow people")
306314

307315
val cat = new Cat
308316
cat.walk()
@@ -321,7 +329,7 @@ The `protected` setting means:
321329

322330
Earlier you saw that traits and classes can have methods.
323331
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.
325333
For instance, this object contains a collection of methods that work on strings:
326334

327335
```scala

_overviews/scala3-book/methods-summary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ There’s even more to know about methods, including how to:
1717
- Handle exceptions
1818
- Use vararg input parameters
1919
- Write methods that have multiple parameter groups (partially-applied functions)
20-
- Create methods that have generic type parameters
20+
- Create methods that have type parameters
2121

2222
See the [Reference documentation][reference] for more details on these features.
2323

0 commit comments

Comments
 (0)