Skip to content

Commit 9860566

Browse files
committed
update formatting
1 parent f817f64 commit 9860566

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

_overviews/scala3-book/taste-control-structures.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ val ints = List(1, 2, 3, 4, 5)
9292
for (i <- ints) println(i)
9393
```
9494

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.
9696
9797
{% endtab %}
9898

@@ -121,7 +121,7 @@ This example prints all of the numbers in `ints` that are greater than `2`:
121121
{% tab 'Scala 2' for=for-guards %}
122122

123123
```scala
124-
for ( i <- ints if i > 2)
124+
for (i <- ints if i > 2)
125125
println(i)
126126
```
127127

@@ -148,9 +148,14 @@ However, it also has two guards, so the only time the print statement is called
148148
{% tab 'Scala 2' for=for-guards-multi %}
149149

150150
```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+
} {
153157
println(s"i = $i, j = $j") // prints: "i = 2, j = b"
158+
}
154159
```
155160

156161
{% endtab %}
@@ -229,7 +234,7 @@ This example shows how to capitalize the first character in each string in the l
229234

230235
```scala
231236
val names = List("chris", "ed", "maurice")
232-
val capNames = for ( name <- names ) yield name.capitalize
237+
val capNames = for (name <- names) yield name.capitalize
233238
```
234239

235240
{% endtab %}
@@ -252,8 +257,8 @@ Finally, this `for` expression iterates over a list of strings, and returns the
252257
```scala
253258
val fruits = List("apple", "banana", "lime", "orange")
254259

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
257262

258263
// fruitLengths: List[Int] = List(5, 6, 6)
259264
```
@@ -387,31 +392,53 @@ p match
387392
In fact, a `match` expression can be used to test a variable against many different types of patterns.
388393
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:
389394

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 %}
392397

393398
```scala
394399
// 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 {
396401
case s: String => s"'$s' is a String"
397402
case i: Int => "Int"
398403
case d: Double => "Double"
399404
case l: List[_] => "List"
400405
case _ => "Unknown"
406+
}
401407

402408
// examples
403409
getClassAsString(1) // Int
404410
getClassAsString("hello") // 'hello' is a String
405411
getClassAsString(List(1, 2, 3)) // List
406412
```
407413

414+
Because the method `getClassAsString` takes a parameter value of type `Any`, it can be decomposed by any kind of
415+
pattern.
416+
408417
{% 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+
```
410434

411435
The method `getClassAsString` takes as a parameter a value of type [Matchable]({{ site.scala3ref }}/other-new-features/matchable.html), which can be
412436
any type supporting pattern matching (some types don’t support pattern matching because this could
413437
break encapsulation).
414438

439+
{% endtab %}
440+
{% endtabs %}
441+
415442
There’s _much_ more to pattern matching in Scala.
416443
Patterns can be nested, results of patterns can be bound, and pattern matching can even be user-defined.
417444
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:
461488
{% tab 'Scala 2' for=while_1 %}
462489

463490
```scala
464-
while ( x >= 0 ) { x = f(x) }
491+
while (x >= 0) { x = f(x) }
465492
```
466493

467494
{% endtab %}

0 commit comments

Comments
 (0)