-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtaste-control-structures.md
541 lines (389 loc) · 12.2 KB
/
taste-control-structures.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
---
title: Control Structures
type: section
description: This section demonstrates Scala 3 control structures.
languages: [ru, zh-cn]
num: 8
previous-page: taste-vars-data-types
next-page: taste-modeling
---
Scala has the control structures you find in other programming languages, and also has powerful `for` expressions and `match` expressions:
- `if`/`else`
- `for` loops and expressions
- `match` expressions
- `while` loops
- `try`/`catch`
These structures are demonstrated in the following examples.
## `if`/`else`
Scala’s `if`/`else` control structure looks similar to other languages.
{% tabs if-else class=tabs-scala-version %}
{% tab 'Scala 2' for=if-else %}
```scala
if (x < 0) {
println("negative")
} else if (x == 0) {
println("zero")
} else {
println("positive")
}
```
{% endtab %}
{% tab 'Scala 3' for=if-else %}
```scala
if x < 0 then
println("negative")
else if x == 0 then
println("zero")
else
println("positive")
```
{% endtab %}
{% endtabs %}
Note that this really is an _expression_---not a _statement_.
This means that it returns a value, so you can assign the result to a variable:
{% tabs if-else-expression class=tabs-scala-version %}
{% tab 'Scala 2' for=if-else-expression %}
```scala
val x = if (a < b) { a } else { b }
```
{% endtab %}
{% tab 'Scala 3' for=if-else-expression %}
```scala
val x = if a < b then a else b
```
{% endtab %}
{% endtabs %}
As you’ll see throughout this book, _all_ Scala control structures can be used as expressions.
> An expression returns a result, while a statement does not.
> Statements are typically used for their side-effects, such as using `println` to print to the console.
## `for` loops and expressions
The `for` keyword is used to create a `for` loop.
This example shows how to print every element in a `List`:
{% tabs for-loop class=tabs-scala-version %}
{% tab 'Scala 2' for=for-loop %}
```scala
val ints = List(1, 2, 3, 4, 5)
for (i <- ints) println(i)
```
> The code `i <- ints` is referred to as a _generator_. In any generator `p <- e`, the expression `e` can generate zero or many bindings to the pattern `p`.
> The code that follows the closing parentheses of the generator is the _body_ of the loop.
{% endtab %}
{% tab 'Scala 3' for=for-loop %}
```scala
val ints = List(1, 2, 3, 4, 5)
for i <- ints do println(i)
```
> The code `i <- ints` is referred to as a _generator_, and the code that follows the `do` keyword is the _body_ of the loop.
{% endtab %}
{% endtabs %}
### Guards
You can also use one or more `if` expressions inside a `for` loop.
These are referred to as _guards_.
This example prints all of the numbers in `ints` that are greater than `2`:
{% tabs for-guards class=tabs-scala-version %}
{% tab 'Scala 2' for=for-guards %}
```scala
for (i <- ints if i > 2)
println(i)
```
{% endtab %}
{% tab 'Scala 3' for=for-guards %}
```scala
for
i <- ints
if i > 2
do
println(i)
```
{% endtab %}
{% endtabs %}
You can use multiple generators and guards.
This loop iterates over the numbers `1` to `3`, and for each number it also iterates over the characters `a` to `c`.
However, it also has two guards, so the only time the print statement is called is when `i` has the value `2` and `j` is the character `b`:
{% tabs for-guards-multi class=tabs-scala-version %}
{% tab 'Scala 2' for=for-guards-multi %}
```scala
for {
i <- 1 to 3
j <- 'a' to 'c'
if i == 2
if j == 'b'
} {
println(s"i = $i, j = $j") // prints: "i = 2, j = b"
}
```
{% endtab %}
{% tab 'Scala 3' for=for-guards-multi %}
```scala
for
i <- 1 to 3
j <- 'a' to 'c'
if i == 2
if j == 'b'
do
println(s"i = $i, j = $j") // prints: "i = 2, j = b"
```
{% endtab %}
{% endtabs %}
### `for` expressions
The `for` keyword has even more power: When you use the `yield` keyword instead of `do`, you create `for` _expressions_ which are used to calculate and yield results.
A few examples demonstrate this.
Using the same `ints` list as the previous example, this code creates a new list, where the value of each element in the new list is twice the value of the elements in the original list:
{% tabs for-expression_1 class=tabs-scala-version %}
{% tab 'Scala 2' for=for-expression_1 %}
````
scala> val doubles = for (i <- ints) yield i * 2
val doubles: List[Int] = List(2, 4, 6, 8, 10)
````
{% endtab %}
{% tab 'Scala 3' for=for-expression_1 %}
````
scala> val doubles = for i <- ints yield i * 2
val doubles: List[Int] = List(2, 4, 6, 8, 10)
````
{% endtab %}
{% endtabs %}
Scala’s control structure syntax is flexible, and that `for` expression can be written in several other ways, depending on your preference:
{% tabs for-expressioni_2 class=tabs-scala-version %}
{% tab 'Scala 2' for=for-expressioni_2 %}
```scala
val doubles = for (i <- ints) yield i * 2
val doubles = for (i <- ints) yield (i * 2)
val doubles = for { i <- ints } yield (i * 2)
```
{% endtab %}
{% tab 'Scala 3' for=for-expressioni_2 %}
```scala
val doubles = for i <- ints yield i * 2 // style shown above
val doubles = for (i <- ints) yield i * 2
val doubles = for (i <- ints) yield (i * 2)
val doubles = for { i <- ints } yield (i * 2)
```
{% endtab %}
{% endtabs %}
This example shows how to capitalize the first character in each string in the list:
{% tabs for-expressioni_3 class=tabs-scala-version %}
{% tab 'Scala 2' for=for-expressioni_3 %}
```scala
val names = List("chris", "ed", "maurice")
val capNames = for (name <- names) yield name.capitalize
```
{% endtab %}
{% tab 'Scala 3' for=for-expressioni_3 %}
```scala
val names = List("chris", "ed", "maurice")
val capNames = for name <- names yield name.capitalize
```
{% endtab %}
{% endtabs %}
Finally, this `for` expression iterates over a list of strings, and returns the length of each string, but only if that length is greater than `4`:
{% tabs for-expressioni_4 class=tabs-scala-version %}
{% tab 'Scala 2' for=for-expressioni_4 %}
```scala
val fruits = List("apple", "banana", "lime", "orange")
val fruitLengths =
for (f <- fruits if f.length > 4) yield f.length
// fruitLengths: List[Int] = List(5, 6, 6)
```
{% endtab %}
{% tab 'Scala 3' for=for-expressioni_4 %}
```scala
val fruits = List("apple", "banana", "lime", "orange")
val fruitLengths = for
f <- fruits
if f.length > 4
yield
// you can use multiple lines
// of code here
f.length
// fruitLengths: List[Int] = List(5, 6, 6)
```
{% endtab %}
{% endtabs %}
`for` loops and expressions are covered in more detail in the [Control Structures sections][control] of this book, and in the [Reference documentation]({{ site.scala3ref }}/other-new-features/control-syntax.html).
## `match` expressions
Scala has a `match` expression, which in its most basic use is like a Java `switch` statement:
{% tabs match class=tabs-scala-version %}
{% tab 'Scala 2' for=match %}
```scala
val i = 1
// later in the code ...
i match {
case 1 => println("one")
case 2 => println("two")
case _ => println("other")
}
```
{% endtab %}
{% tab 'Scala 3' for=match %}
```scala
val i = 1
// later in the code ...
i match
case 1 => println("one")
case 2 => println("two")
case _ => println("other")
```
{% endtab %}
{% endtabs %}
However, `match` really is an expression, meaning that it returns a result based on the pattern match, which you can bind to a variable:
{% tabs match-expression_1 class=tabs-scala-version %}
{% tab 'Scala 2' for=match-expression_1 %}
```scala
val result = i match {
case 1 => "one"
case 2 => "two"
case _ => "other"
}
```
{% endtab %}
{% tab 'Scala 3' for=match-expression_1 %}
```scala
val result = i match
case 1 => "one"
case 2 => "two"
case _ => "other"
```
{% endtab %}
{% endtabs %}
`match` isn’t limited to working with just integer values, it can be used with any data type:
{% tabs match-expression_2 class=tabs-scala-version %}
{% tab 'Scala 2' for=match-expression_2 %}
```scala
val p = Person("Fred")
// later in the code
p match {
case Person(name) if name == "Fred" =>
println(s"$name says, Yubba dubba doo")
case Person(name) if name == "Bam Bam" =>
println(s"$name says, Bam bam!")
case _ => println("Watch the Flintstones!")
}
```
{% endtab %}
{% tab 'Scala 3' for=match-expression_2 %}
```scala
val p = Person("Fred")
// later in the code
p match
case Person(name) if name == "Fred" =>
println(s"$name says, Yubba dubba doo")
case Person(name) if name == "Bam Bam" =>
println(s"$name says, Bam bam!")
case _ => println("Watch the Flintstones!")
```
{% endtab %}
{% endtabs %}
In fact, a `match` expression can be used to test a variable against many different types of patterns.
This example shows (a) how to use a `match` expression as the body of a method, and (b) how to match all the different types shown:
{% tabs match-expression_3 class=tabs-scala-version %}
{% tab 'Scala 2' for=match-expression_3 %}
```scala
// getClassAsString is a method that takes a single argument of any type.
def getClassAsString(x: Any): String = x match {
case s: String => s"'$s' is a String"
case i: Int => "Int"
case d: Double => "Double"
case l: List[_] => "List"
case _ => "Unknown"
}
// examples
getClassAsString(1) // Int
getClassAsString("hello") // 'hello' is a String
getClassAsString(List(1, 2, 3)) // List
```
Because the method `getClassAsString` takes a parameter value of type `Any`, it can be decomposed by any kind of
pattern.
{% endtab %}
{% tab 'Scala 3' for=match-expression_3 %}
```scala
// getClassAsString is a method that takes a single argument of any type.
def getClassAsString(x: Matchable): String = x match
case s: String => s"'$s' is a String"
case i: Int => "Int"
case d: Double => "Double"
case l: List[?] => "List"
case _ => "Unknown"
// examples
getClassAsString(1) // Int
getClassAsString("hello") // 'hello' is a String
getClassAsString(List(1, 2, 3)) // List
```
The method `getClassAsString` takes as a parameter a value of type [Matchable]({{ site.scala3ref }}/other-new-features/matchable.html), which can be
any type supporting pattern matching (some types don’t support pattern matching because this could
break encapsulation).
{% endtab %}
{% endtabs %}
There’s _much_ more to pattern matching in Scala.
Patterns can be nested, results of patterns can be bound, and pattern matching can even be user-defined.
See the pattern matching examples in the [Control Structures chapter][control] for more details.
## `try`/`catch`/`finally`
Scala’s `try`/`catch`/`finally` control structure lets you catch exceptions.
It’s similar to Java, but its syntax is consistent with `match` expressions:
{% tabs try class=tabs-scala-version %}
{% tab 'Scala 2' for=try %}
```scala
try {
writeTextToFile(text)
} catch {
case ioe: IOException => println("Got an IOException.")
case nfe: NumberFormatException => println("Got a NumberFormatException.")
} finally {
println("Clean up your resources here.")
}
```
{% endtab %}
{% tab 'Scala 3' for=try %}
```scala
try
writeTextToFile(text)
catch
case ioe: IOException => println("Got an IOException.")
case nfe: NumberFormatException => println("Got a NumberFormatException.")
finally
println("Clean up your resources here.")
```
{% endtab %}
{% endtabs %}
## `while` loops
Scala also has a `while` loop construct.
Its one-line syntax looks like this:
{% tabs while_1 class=tabs-scala-version %}
{% tab 'Scala 2' for=while_1 %}
```scala
while (x >= 0) { x = f(x) }
```
{% endtab %}
{% tab 'Scala 3' for=while_1 %}
```scala
while x >= 0 do x = f(x)
```
Scala 3 still supports the Scala 2 syntax for the sake of compatibility.
{% endtab %}
{% endtabs %}
The `while` loop multiline syntax looks like this:
{% tabs while_2 class=tabs-scala-version %}
{% tab 'Scala 2' for=while_2 %}
```scala
var x = 1
while (x < 3) {
println(x)
x += 1
}
```
{% endtab %}
{% tab 'Scala 3' for=while_2 %}
```scala
var x = 1
while
x < 3
do
println(x)
x += 1
```
{% endtab %}
{% endtabs %}
## Custom control structures
Thanks to features like by-name parameters, infix notation, fluent interfaces, optional parentheses, extension methods, and higher-order functions, you can also create your own code that works just like a control structure.
You’ll learn more about this in the [Control Structures][control] section.
[control]: {% link _overviews/scala3-book/control-structures.md %}