@@ -92,7 +92,7 @@ val ints = List(1, 2, 3, 4, 5)
92
92
for (i <- ints) println(i)
93
93
```
94
94
95
- > The old syntax in Scala 2 ` for ` structure .
95
+ > The code ` i <- ints ` is referred to as a _ generator _ , and the code that follows the closing parentheses of the generator is the _ body _ of the loop .
96
96
97
97
{% endtab %}
98
98
@@ -109,8 +109,6 @@ for i <- ints do println(i)
109
109
{% endtab %}
110
110
{% endtabs %}
111
111
112
- Again, note the usage of parentheses and the new ` for ` -` do ` in Scala 3.
113
-
114
112
### Guards
115
113
116
114
You can also use one or more ` if ` expressions inside a ` for ` loop.
@@ -121,7 +119,7 @@ This example prints all of the numbers in `ints` that are greater than `2`:
121
119
{% tab 'Scala 2' for=for-guards %}
122
120
123
121
``` scala
124
- for ( i <- ints if i > 2 )
122
+ for (i <- ints if i > 2 )
125
123
println(i)
126
124
```
127
125
@@ -148,9 +146,14 @@ However, it also has two guards, so the only time the print statement is called
148
146
{% tab 'Scala 2' for=for-guards-multi %}
149
147
150
148
``` scala
151
- for ( i <- 1 to 3 if i == 2 ;
152
- j <- 'a' to 'c' if j == 'b' )
149
+ for {
150
+ i <- 1 to 3
151
+ j <- 'a' to 'c'
152
+ if i == 2
153
+ if j == 'b'
154
+ } {
153
155
println(s " i = $i, j = $j" ) // prints: "i = 2, j = b"
156
+ }
154
157
```
155
158
156
159
{% endtab %}
@@ -229,7 +232,7 @@ This example shows how to capitalize the first character in each string in the l
229
232
230
233
``` scala
231
234
val names = List (" chris" , " ed" , " maurice" )
232
- val capNames = for ( name <- names ) yield name.capitalize
235
+ val capNames = for (name <- names) yield name.capitalize
233
236
```
234
237
235
238
{% endtab %}
@@ -252,8 +255,8 @@ Finally, this `for` expression iterates over a list of strings, and returns the
252
255
``` scala
253
256
val fruits = List (" apple" , " banana" , " lime" , " orange" )
254
257
255
- val fruitLengths = for ( f <- fruits if f.length > 4 )
256
- yield f.length
258
+ val fruitLengths =
259
+ for (f <- fruits if f.length > 4 ) yield f.length
257
260
258
261
// fruitLengths: List[Int] = List(5, 6, 6)
259
262
```
@@ -387,31 +390,53 @@ p match
387
390
In fact, a ` match ` expression can be used to test a variable against many different types of patterns.
388
391
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:
389
392
390
- {% tabs match-expression_3 %}
391
- {% tab 'Scala 3 Only ' for=match-expression_3 %}
393
+ {% tabs match-expression_3 class=tabs-scala-version %}
394
+ {% tab 'Scala 2 ' for=match-expression_3 %}
392
395
393
396
``` scala
394
397
// getClassAsString is a method that takes a single argument of any type.
395
- def getClassAsString (x : Matchable ): String = x match
398
+ def getClassAsString (x : Any ): String = x match {
396
399
case s : String => s " ' $s' is a String "
397
400
case i : Int => " Int"
398
401
case d : Double => " Double"
399
402
case l : List [_] => " List"
400
403
case _ => " Unknown"
404
+ }
401
405
402
406
// examples
403
407
getClassAsString(1 ) // Int
404
408
getClassAsString(" hello" ) // 'hello' is a String
405
409
getClassAsString(List (1 , 2 , 3 )) // List
406
410
```
407
411
412
+ Because the method ` getClassAsString ` takes a parameter value of type ` Any ` , it can be decomposed by any kind of
413
+ pattern.
414
+
408
415
{% endtab %}
409
- {% endtabs %}
416
+ {% tab 'Scala 3' for=match-expression_3 %}
417
+
418
+ ``` scala
419
+ // getClassAsString is a method that takes a single argument of any type.
420
+ def getClassAsString (x : Matchable ): String = x match
421
+ case s : String => s " ' $s' is a String "
422
+ case i : Int => " Int"
423
+ case d : Double => " Double"
424
+ case l : List [? ] => " List"
425
+ case _ => " Unknown"
426
+
427
+ // examples
428
+ getClassAsString(1 ) // Int
429
+ getClassAsString(" hello" ) // 'hello' is a String
430
+ getClassAsString(List (1 , 2 , 3 )) // List
431
+ ```
410
432
411
433
The method ` getClassAsString ` takes as a parameter a value of type [ Matchable] ({{ site.scala3ref }}/other-new-features/matchable.html), which can be
412
434
any type supporting pattern matching (some types don’t support pattern matching because this could
413
435
break encapsulation).
414
436
437
+ {% endtab %}
438
+ {% endtabs %}
439
+
415
440
There’s _ much_ more to pattern matching in Scala.
416
441
Patterns can be nested, results of patterns can be bound, and pattern matching can even be user-defined.
417
442
See the pattern matching examples in the [ Control Structures chapter] [ control ] for more details.
@@ -461,7 +486,7 @@ It’s one-line syntax looks like this:
461
486
{% tab 'Scala 2' for=while_1 %}
462
487
463
488
``` scala
464
- while ( x >= 0 ) { x = f(x) }
489
+ while (x >= 0 ) { x = f(x) }
465
490
```
466
491
467
492
{% endtab %}
@@ -471,12 +496,11 @@ while ( x >= 0 ) { x = f(x) }
471
496
``` scala
472
497
while x >= 0 do x = f(x)
473
498
```
499
+ Scala 3 still supports the Scala 2 syntax for the sake of compatibility.
474
500
475
501
{% endtab %}
476
502
{% endtabs %}
477
503
478
- Scala 3 still supports the Scala 2 syntax for the sake of compatibility.
479
-
480
504
The ` while ` loop multiline syntax looks like this:
481
505
482
506
{% tabs while_2 class=tabs-scala-version %}
0 commit comments