@@ -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
@@ -121,7 +121,7 @@ This example prints all of the numbers in `ints` that are greater than `2`:
121
121
{% tab 'Scala 2' for=for-guards %}
122
122
123
123
``` scala
124
- for ( i <- ints if i > 2 )
124
+ for (i <- ints if i > 2 )
125
125
println(i)
126
126
```
127
127
@@ -148,9 +148,14 @@ However, it also has two guards, so the only time the print statement is called
148
148
{% tab 'Scala 2' for=for-guards-multi %}
149
149
150
150
``` scala
151
- for ( i <- 1 to 3 if i == 2 ;
152
- j <- 'a' to 'c' if j == 'b' )
151
+ for {
152
+ i <- 1 to 3
153
+ j <- 'a' to 'c'
154
+ if i == 2
155
+ if j == 'b'
156
+ } {
153
157
println(s " i = $i, j = $j" ) // prints: "i = 2, j = b"
158
+ }
154
159
```
155
160
156
161
{% endtab %}
@@ -229,7 +234,7 @@ This example shows how to capitalize the first character in each string in the l
229
234
230
235
``` scala
231
236
val names = List (" chris" , " ed" , " maurice" )
232
- val capNames = for ( name <- names ) yield name.capitalize
237
+ val capNames = for (name <- names) yield name.capitalize
233
238
```
234
239
235
240
{% endtab %}
@@ -252,8 +257,8 @@ Finally, this `for` expression iterates over a list of strings, and returns the
252
257
``` scala
253
258
val fruits = List (" apple" , " banana" , " lime" , " orange" )
254
259
255
- val fruitLengths = for ( f <- fruits if f.length > 4 )
256
- yield f.length
260
+ val fruitLengths =
261
+ for (f <- fruits if f.length > 4 ) yield f.length
257
262
258
263
// fruitLengths: List[Int] = List(5, 6, 6)
259
264
```
@@ -387,31 +392,53 @@ p match
387
392
In fact, a ` match ` expression can be used to test a variable against many different types of patterns.
388
393
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
394
390
- {% tabs match-expression_3 %}
391
- {% tab 'Scala 3 Only ' for=match-expression_3 %}
395
+ {% tabs match-expression_3 class=tabs-scala-version %}
396
+ {% tab 'Scala 2 ' for=match-expression_3 %}
392
397
393
398
``` scala
394
399
// getClassAsString is a method that takes a single argument of any type.
395
- def getClassAsString (x : Matchable ): String = x match
400
+ def getClassAsString (x : Any ): String = x match {
396
401
case s : String => s " ' $s' is a String "
397
402
case i : Int => " Int"
398
403
case d : Double => " Double"
399
404
case l : List [_] => " List"
400
405
case _ => " Unknown"
406
+ }
401
407
402
408
// examples
403
409
getClassAsString(1 ) // Int
404
410
getClassAsString(" hello" ) // 'hello' is a String
405
411
getClassAsString(List (1 , 2 , 3 )) // List
406
412
```
407
413
414
+ Because the method ` getClassAsString ` takes a parameter value of type ` Any ` , it can be decomposed by any kind of
415
+ pattern.
416
+
408
417
{% endtab %}
409
- {% endtabs %}
418
+ {% tab 'Scala 3' for=match-expression_3 %}
419
+
420
+ ``` scala
421
+ // getClassAsString is a method that takes a single argument of any type.
422
+ def getClassAsString (x : Matchable ): String = x match
423
+ case s : String => s " ' $s' is a String "
424
+ case i : Int => " Int"
425
+ case d : Double => " Double"
426
+ case l : List [? ] => " List"
427
+ case _ => " Unknown"
428
+
429
+ // examples
430
+ getClassAsString(1 ) // Int
431
+ getClassAsString(" hello" ) // 'hello' is a String
432
+ getClassAsString(List (1 , 2 , 3 )) // List
433
+ ```
410
434
411
435
The method ` getClassAsString ` takes as a parameter a value of type [ Matchable] ({{ site.scala3ref }}/other-new-features/matchable.html), which can be
412
436
any type supporting pattern matching (some types don’t support pattern matching because this could
413
437
break encapsulation).
414
438
439
+ {% endtab %}
440
+ {% endtabs %}
441
+
415
442
There’s _ much_ more to pattern matching in Scala.
416
443
Patterns can be nested, results of patterns can be bound, and pattern matching can even be user-defined.
417
444
See the pattern matching examples in the [ Control Structures chapter] [ control ] for more details.
@@ -461,7 +488,7 @@ It’s one-line syntax looks like this:
461
488
{% tab 'Scala 2' for=while_1 %}
462
489
463
490
``` scala
464
- while ( x >= 0 ) { x = f(x) }
491
+ while (x >= 0 ) { x = f(x) }
465
492
```
466
493
467
494
{% endtab %}
0 commit comments