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/domain-modeling-tools.md
+81-48Lines changed: 81 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ val p = new Person("Robert Allen Zimmerman", "Harmonica Player")
57
57
{% endtab %}
58
58
{% endtabs %}
59
59
60
-
However, with [creator applications][creator] this isn’t required in Scala 3:
60
+
However, with [universal apply methods][creator] this isn’t required in Scala 3:
61
61
62
62
{% tabs class_3 %}
63
63
{% tab 'Scala 3 Only' for=class_3 %}
@@ -144,7 +144,21 @@ class Person(var firstName: String, var lastName: String):
144
144
145
145
The following REPL session shows how to create a new `Person` instance with this class:
146
146
147
+
{% tabs demo-person class=tabs-scala-version %}
148
+
{% tab 'Scala 2' for=demo-person %}
149
+
````scala
150
+
scala>valjohn=newPerson("John", "Doe")
151
+
initialization begins
152
+
JohnDoe
153
+
initialization ends
154
+
valjohn:Person=Person@55d8f6bb
155
+
156
+
scala> john.printFullName
157
+
JohnDoe
147
158
````
159
+
{% endtab %}
160
+
{% tab 'Scala 3' for=demo-person %}
161
+
````scala
148
162
scala>valjohn=Person("John", "Doe")
149
163
initialization begins
150
164
JohnDoe
@@ -154,6 +168,8 @@ val john: Person = Person@55d8f6bb
154
168
scala> john.printFullName
155
169
JohnDoe
156
170
````
171
+
{% endtab %}
172
+
{% endtabs %}
157
173
158
174
Classes can also extend traits and abstract classes, which we cover in dedicated sections below.
159
175
@@ -184,8 +200,19 @@ class Socket(val timeout: Int = 5_000, val linger: Int = 5_000):
184
200
185
201
A great thing about this feature is that it lets consumers of your code create classes in a variety of different ways, as though the class had alternate constructors:
The `unapply` method isn’t covered here, but it’s covered in the [Language Specification](https://scala-lang.org/files/archive/spec/2.13/08-pattern-matching.html#extractor-patterns).
608
+
566
609
{% endtab %}
567
610
568
611
{% tab 'Scala 3' for=companion-use %}
@@ -597,11 +640,11 @@ val fred = Person("Fred", 29)
597
640
//val fred: Person = Fred is 29 years old
598
641
```
599
642
643
+
The `unapply` method isn’t covered here, but it’s covered in the [Reference documentation]({{ site.scala3ref }}/changed-features/pattern-matching.html).
644
+
600
645
{% endtab %}
601
646
{% endtabs %}
602
647
603
-
The `unapply` method isn’t covered here, but it’s covered in the [Reference documentation][unapply].
604
-
605
648
## Traits
606
649
607
650
If you’re familiar with Java, a Scala trait is similar to an interface in Java 8+. Traits can contain:
@@ -700,7 +743,7 @@ Later in your code, classes can mix multiple traits to build larger components:
@@ -1156,7 +1189,7 @@ case Teacher(name, whatTheyTeach) =>
1156
1189
{% endtabs %}
1157
1190
1158
1191
Those patterns work because `Student` and `Teacher` are defined as case classes that have `unapply` methods whose type signature conforms to a certain standard.
1159
-
Technically, the specific type of pattern case-classes_1ing shown in these examples is known as a _constructor pattern_.
1192
+
Technically, the specific type of pattern matching shown in these examples is known as a _constructor pattern_.
1160
1193
1161
1194
> The Scala standard is that an `unapply` method returns the case class constructor fields in a tuple that’s wrapped in an `Option`.
1162
1195
> The “tuple” part of the solution was shown in the previous lesson.
> All of this content on `unapply` methods and extractors is a little advanced for an introductory book like this, but because case classes are an important FP topic, it seems better to cover them, rather than skipping over them.
1194
1227
1195
-
#### Add pattern case-classes_1ing to any type with unapply
1228
+
#### Add pattern matching to any type with unapply
1196
1229
1197
-
A great Scala feature is that you can add pattern case-classes_1ing to any type by writing your own `unapply` method.
1230
+
A great Scala feature is that you can add pattern matching to any type by writing your own `unapply` method.
1198
1231
As an example, this class defines an `unapply` method in its companion object:
@@ -1277,7 +1310,7 @@ case object StopPlaying extends Message
1277
1310
{% endtab %}
1278
1311
{% endtabs %}
1279
1312
1280
-
Then in other parts of your code, you can write methods like this, which use pattern case-objects_1ing to handle the incoming message (assuming the methods `playSong`, `changeVolume`, and `stopPlayingSong` are defined somewhere else):
1313
+
Then in other parts of your code, you can write methods like this, which use pattern matching to handle the incoming message (assuming the methods `playSong`, `changeVolume`, and `stopPlayingSong` are defined somewhere else):
0 commit comments