Skip to content

Commit d2e2e36

Browse files
committed
add scala2 code.
1 parent 2a9c00e commit d2e2e36

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

_overviews/scala3-book/taste-modeling.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ As an example of how to use traits as interfaces, here are three traits that def
3737

3838
{% tabs traits class=tabs-scala-version %}
3939
{% tab 'Scala 2' for=traits %}
40+
4041
```scala
4142
trait Speaker {
4243
def speak(): String // has no body, so it’s abstract
@@ -52,9 +53,11 @@ trait Runner {
5253
def stopRunning(): Unit = println("Stopped running")
5354
}
5455
```
56+
5557
{% endtab %}
5658

5759
{% tab 'Scala 3' for=traits %}
60+
5861
```scala
5962
trait Speaker:
6063
def speak(): String // has no body, so it’s abstract
@@ -67,25 +70,30 @@ trait Runner:
6770
def startRunning(): Unit = println("I’m running")
6871
def stopRunning(): Unit = println("Stopped running")
6972
```
73+
7074
{% endtab %}
7175
{% endtabs %}
7276

7377
Given those traits, here’s a `Dog` class that extends all of those traits while providing a behavior for the abstract `speak` method:
7478

7579
{% tabs traits-class class=tabs-scala-version %}
7680
{% tab 'Scala 2' for=traits-class %}
81+
7782
```scala
7883
class Dog(name: String) extends Speaker with TailWagger with Runner {
7984
def speak(): String = "Woof!"
8085
}
8186
```
87+
8288
{% endtab %}
8389

8490
{% tab 'Scala 3' for=traits-class %}
91+
8592
```scala
8693
class Dog(name: String) extends Speaker, TailWagger, Runner:
8794
def speak(): String = "Woof!"
8895
```
96+
8997
{% endtab %}
9098
{% endtabs %}
9199

@@ -95,29 +103,48 @@ Similarly, here’s a `Cat` class that implements those same traits while also o
95103

96104
{% tabs traits-override class=tabs-scala-version %}
97105
{% tab 'Scala 2' for=traits-override %}
106+
98107
```scala
99108
class Cat(name: String) extends Speaker with TailWagger with Runner {
100109
def speak(): String = "Meow"
101110
override def startRunning(): Unit = println("Yeah ... I don’t run")
102111
override def stopRunning(): Unit = println("No need to stop")
103112
}
104113
```
114+
105115
{% endtab %}
106116

107117
{% tab 'Scala 3' for=traits-override %}
118+
108119
```scala
109120
class Cat(name: String) extends Speaker, TailWagger, Runner:
110121
def speak(): String = "Meow"
111122
override def startRunning(): Unit = println("Yeah ... I don’t run")
112123
override def stopRunning(): Unit = println("No need to stop")
113124
```
125+
114126
{% endtab %}
115127
{% endtabs %}
116128

117129
These examples show how those classes are used:
118130

119131
{% tabs traits-use class=tabs-scala-version %}
120-
{% tab 'Scala 2 and 3' for=traits-use %}
132+
{% tab 'Scala 2' for=traits-use %}
133+
134+
```scala
135+
val d = new Dog("Rover")
136+
println(d.speak()) // prints "Woof!"
137+
138+
val c = new Cat("Morris")
139+
println(c.speak()) // "Meow"
140+
c.startRunning() // "Yeah ... I don’t run"
141+
c.stopRunning() // "No need to stop"
142+
```
143+
144+
{% endtab %}
145+
146+
{% tab 'Scala 3' for=traits-use %}
147+
121148
```scala
122149
val d = Dog("Rover")
123150
println(d.speak()) // prints "Woof!"
@@ -127,6 +154,7 @@ println(c.speak()) // "Meow"
127154
c.startRunning() // "Yeah ... I don’t run"
128155
c.stopRunning() // "No need to stop"
129156
```
157+
130158
{% endtab %}
131159
{% endtabs %}
132160

@@ -140,6 +168,7 @@ Here’s an example of a class that models a “person.” In OOP fields are typ
140168

141169
{% tabs class_1 class=tabs-scala-version %}
142170
{% tab 'Scala 2' for=class_1 %}
171+
143172
```scala
144173
class Person(var firstName: String, var lastName: String) {
145174
def printFullName() = println(s"$firstName $lastName")
@@ -150,9 +179,11 @@ println(p.firstName) // "John"
150179
p.lastName = "Legend"
151180
p.printFullName() // "John Legend"
152181
```
182+
153183
{% endtab %}
154184

155185
{% tab 'Scala 3' for=class_1 %}
186+
156187
```scala
157188
class Person(var firstName: String, var lastName: String):
158189
def printFullName() = println(s"$firstName $lastName")
@@ -162,17 +193,29 @@ println(p.firstName) // "John"
162193
p.lastName = "Legend"
163194
p.printFullName() // "John Legend"
164195
```
196+
165197
{% endtab %}
166198
{% endtabs %}
167199

168200
Notice that the class declaration creates a constructor:
169201

170202
{% tabs class_2 class=tabs-scala-version %}
171-
{% tab 'Scala 2 and 3' for=class_2 %}
203+
{% tab 'Scala 2' for=class_2 %}
204+
205+
```scala
206+
// this code uses that constructor
207+
val p = new Person("John", "Stephens")
208+
```
209+
210+
{% endtab %}
211+
212+
{% tab 'Scala 3' for=class_2 %}
213+
172214
```scala
173215
// this code uses that constructor
174216
val p = Person("John", "Stephens")
175217
```
218+
176219
{% endtab %}
177220
{% endtabs %}
178221

@@ -203,8 +246,9 @@ For instance, a pizza has three main attributes:
203246

204247
These are concisely modeled with enums:
205248

206-
{% tabs enum_1 class=tabs-scala-version %}
207-
{% tab 'Scala 3 only' for=enum_1 %}
249+
{% tabs enum_1 %}
250+
{% tab 'Scala 3 Only' for=enum_1 %}
251+
208252
```scala
209253
enum CrustSize:
210254
case Small, Medium, Large
@@ -215,13 +259,15 @@ enum CrustType:
215259
enum Topping:
216260
case Cheese, Pepperoni, BlackOlives, GreenOlives, Onions
217261
```
262+
218263
{% endtab %}
219264
{% endtabs %}
220265

221266
Once you have an enum you can use it in all of the ways you normally use a trait, class, or object:
222267

223-
{% tabs enum_2 class=tabs-scala-version %}
224-
{% tab 'Scala 3 only' for=enum_2 %}
268+
{% tabs enum_2 %}
269+
{% tab 'Scala 3 Only' for=enum_2 %}
270+
225271
```scala
226272
import CrustSize.*
227273
val currentCrustSize = Small
@@ -235,18 +281,21 @@ currentCrustSize match
235281
// enums in an `if` statement
236282
if currentCrustSize == Small then println("Small crust size")
237283
```
284+
238285
{% endtab %}
239286
{% endtabs %}
240287

241288
Here’s another example of how to create and use an ADT with Scala:
242289

243-
{% tabs enum_3 class=tabs-scala-version %}
244-
{% tab 'Scala 3 only' for=enum_3 %}
290+
{% tabs enum_3 %}
291+
{% tab 'Scala 3 Only' for=enum_3 %}
292+
245293
```scala
246294
enum Nat:
247295
case Zero
248296
case Succ(pred: Nat)
249297
```
298+
250299
{% endtab %}
251300
{% endtabs %}
252301

@@ -275,6 +324,7 @@ This code demonstrates several `case` class features:
275324

276325
{% tabs case-class class=tabs-scala-version %}
277326
{% tab 'Scala 2 and 3' for=case-class %}
327+
278328
```scala
279329
// define a case class
280330
case class Person(
@@ -297,6 +347,7 @@ p.name = "Joe" // error: can’t reassign a val field
297347
val p2 = p.copy(name = "Elton John")
298348
p2 // : Person = Person(Elton John,Singer)
299349
```
350+
300351
{% endtab %}
301352
{% endtabs %}
302353

0 commit comments

Comments
 (0)