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
The examples above show that Scala has a very lightweight way to declare classes.
36
-
The definition of the class `Person` roughly corresponds to the following, more explicit, version
35
+
36
+
These examples show that Scala has a very lightweight way to declare classes.
37
+
The definition of the class `Person` roughly corresponds to the following, more explicit, version:
37
38
38
39
```scala
39
40
classPerson:
@@ -49,7 +50,8 @@ class Person:
49
50
name = _name
50
51
vocation = _vocation
51
52
```
52
-
defining the two fields `name` and `vocation` together with a constructor that accepts values for the two fields and assigns them.
53
+
54
+
This version defines the two fields `name` and `vocation`, together with a constructor that accepts values for those two fields and assigns them.
53
55
54
56
All of the parameters of our example classes are defined as `var` fields, which means they are mutable: you can read them, and also modify them.
55
57
If you want them to be immutable---read only---create them as `val` fields instead.
@@ -67,7 +69,7 @@ However, with [creator applications][creator] this isn’t required in Scala 3:
67
69
valp=Person("Robert Allen Zimmerman", "Harmonica Player")
68
70
```
69
71
70
-
Once you have an instance of a class, you can access its fields, which in this example are all constructor parameters:
72
+
Once you have an instance of a class such as `p`, you can access its fields, which in this example are all constructor parameters:
71
73
72
74
```scala
73
75
p.name // "Robert Allen Zimmerman"
@@ -227,7 +229,7 @@ This is shown in the previous `Socket` example.
227
229
228
230
An object is a class that has exactly one instance.
229
231
It’s initialized lazily when its members are referenced, similar to a `lazy val`.
230
-
Objects in Scala allow grouping methods and fields under one namespace, similar to how can use `static` members on a class in Java, Javascript (ES6) or `@staticmethod` in Python.
232
+
Objects in Scala allow grouping methods and fields under one namespace, similar to how you use `static` members on a class in Java, Javascript (ES6), or `@staticmethod` in Python.
231
233
232
234
Declaring an `object` is similar to declaring a `class`.
233
235
Here’s an example of a “string utilities” object that contains a set of methods for working with strings:
@@ -245,11 +247,22 @@ We can use the object as follows:
An `object` that has the same name as a class, and is declared in the same file as the class, is called a _"companion object"_.
282
+
An `object` that has the same name as a class, and is declared in the same file as the class, is called a _"companion object_."
270
283
Similarly, the corresponding class is called the object’s companion class.
271
284
A companion class or object can access the private members of its companion.
272
285
@@ -287,7 +300,7 @@ circle1.area
287
300
```
288
301
289
302
In this example the `area` method that’s available to each instance uses the `calculateArea` method that’s defined in the companion object.
290
-
If you’re familiar with Java, `calculateArea` is similar to a static method.
303
+
Once again, `calculateArea` is similar to a static method in Java.
291
304
Also, because `calculateArea` is private, it can’t be accessed by other code, but as shown, it can be seen by instances of the `Circle` class.
292
305
293
306
### Other uses
@@ -300,7 +313,7 @@ Companion objects can be used for several purposes:
300
313
- They can contain `apply` methods, which---thanks to some syntactic sugar---work as factory methods to construct new instances
301
314
- They can contain `unapply` methods, which are used to deconstruct objects, such as with pattern matching
302
315
303
-
Here’s a quick look at how `apply` methods that can be used as factory methods to create new objects:
316
+
Here’s a quick look at how `apply` methods can be used as factory methods to create new objects:
304
317
305
318
```scala
306
319
classPerson:
@@ -436,8 +449,8 @@ class Dog(name: String, var age: Int) extends Pet(name):
436
449
437
450
vald=Dog("Fido", 1)
438
451
```
439
-
Traits are more flexible to compose (you can mix in multiple traits, but only extend one class) and should most of the time be preferred to classes.
440
-
The rule of thumb is to use classes whenever you want to create instances of a particular type and traits when you want to decompose and reuse behaviour.
452
+
Traits are more flexible to compose --- you can mix in multiple traits, but only extend one class --- and should be preferred to classes and abstract classes most of the time.
453
+
The rule of thumb is to use classes whenever you want to create instances of a particular type, and traits when you want to decompose and reuse behaviour.
441
454
442
455
443
456
## Enums
@@ -466,7 +479,7 @@ import CrustSize._
466
479
valcurrentCrustSize=Small
467
480
```
468
481
469
-
Enum values can be compared using equals (`==`) and matched on:
482
+
Enum values can be compared using equals (`==`), and also matched on:
470
483
471
484
```scala
472
485
// if/then
@@ -529,11 +542,11 @@ The section on [algebraic datatypes][adts] and the [reference documentation][ref
529
542
## Case classes
530
543
531
544
Case classes are used to model immutable data structures.
532
-
Take the following example,
545
+
Take the following example:
533
546
```scala
534
547
caseclassPerson(name: String, relation: String)
535
548
```
536
-
Since we declared`Person` as a case class, the fields `name` and `relation` are public and immutable by default.
549
+
Since we declare`Person` as a case class, the fields `name` and `relation` are public and immutable by default.
537
550
We can create instances of case classes as follows:
538
551
```scala
539
552
valchristina=Person("Christina", "niece")
@@ -544,9 +557,9 @@ christina.name = "Fred" // error: reassignment to val
544
557
```
545
558
Since the fields of a case class are assumed to be immutable, the Scala compiler can generate many helpful methods for you:
546
559
* An `unapply` method is generated, which allows you to perform pattern matching on a case class (that is, `case Person(n, r) => ...`).
547
-
* A `copy` method is generated in the class, which is very useful to create modified copies of an instance
560
+
* A `copy` method is generated in the class, which is very useful to create modified copies of an instance.
548
561
*`equals` and `hashCode` methods using structural equality are generated, allowing you to use instances of case classes in `Map`s.
549
-
* A default `toString` method is generated, which is helpful for debugging
562
+
* A default `toString` method is generated, which is helpful for debugging.
550
563
551
564
These additional features are demonstrated in the below example:
552
565
```scala
@@ -576,8 +589,8 @@ As mentioned, case classes support functional programming (FP):
576
589
577
590
- In FP you try to avoid mutating data structures.
578
591
It thus makes sense that constructor fields default to `val`.
579
-
Since instances of case classes are not changed, they can easily be shared without fearing mutation or race conditions.
580
-
- Instead of mutating one instance, you can use the `copy` method as a template to create a new (potentially changed) instance.
592
+
Since instances of case classes can’t be changed, they can easily be shared without fearing mutation or race conditions.
593
+
- Instead of mutating an instance, you can use the `copy` method as a template to create a new (potentially changed) instance.
581
594
This process can be referred to as “update as you copy.”
582
595
- Having an `unapply` method auto-generated for you also lets case classes be used in advanced ways with pattern matching.
583
596
@@ -621,7 +634,7 @@ case Teacher(name, whatTheyTeach) =>
621
634
```
622
635
623
636
Those patterns work because `Student` and `Teacher` are defined as case classes that have `unapply` methods whose type signature conforms to a certain standard.
624
-
Technically, the specific type of pattern matching shown in these examples is known as a *constructor pattern*.
637
+
Technically, the specific type of pattern matching shown in these examples is known as a _constructor pattern_.
625
638
626
639
> The Scala standard is that an `unapply` method returns the case class constructor fields in a tuple that’s wrapped in an `Option`.
627
640
> The “tuple” part of the solution was shown in the previous lesson.
0 commit comments