Skip to content

Commit d7ed15c

Browse files
committed
update formatting
1 parent f817f64 commit d7ed15c

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

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

Lines changed: 40 additions & 16 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

@@ -109,8 +109,6 @@ for i <- ints do println(i)
109109
{% endtab %}
110110
{% endtabs %}
111111

112-
Again, note the usage of parentheses and the new `for`-`do` in Scala 3.
113-
114112
### Guards
115113

116114
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`:
121119
{% tab 'Scala 2' for=for-guards %}
122120

123121
```scala
124-
for ( i <- ints if i > 2)
122+
for (i <- ints if i > 2)
125123
println(i)
126124
```
127125

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

150148
```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+
} {
153155
println(s"i = $i, j = $j") // prints: "i = 2, j = b"
156+
}
154157
```
155158

156159
{% endtab %}
@@ -229,7 +232,7 @@ This example shows how to capitalize the first character in each string in the l
229232

230233
```scala
231234
val names = List("chris", "ed", "maurice")
232-
val capNames = for ( name <- names ) yield name.capitalize
235+
val capNames = for (name <- names) yield name.capitalize
233236
```
234237

235238
{% endtab %}
@@ -252,8 +255,8 @@ Finally, this `for` expression iterates over a list of strings, and returns the
252255
```scala
253256
val fruits = List("apple", "banana", "lime", "orange")
254257

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
257260

258261
// fruitLengths: List[Int] = List(5, 6, 6)
259262
```
@@ -387,31 +390,53 @@ p match
387390
In fact, a `match` expression can be used to test a variable against many different types of patterns.
388391
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:
389392

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

393396
```scala
394397
// 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 {
396399
case s: String => s"'$s' is a String"
397400
case i: Int => "Int"
398401
case d: Double => "Double"
399402
case l: List[_] => "List"
400403
case _ => "Unknown"
404+
}
401405

402406
// examples
403407
getClassAsString(1) // Int
404408
getClassAsString("hello") // 'hello' is a String
405409
getClassAsString(List(1, 2, 3)) // List
406410
```
407411

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

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

437+
{% endtab %}
438+
{% endtabs %}
439+
415440
There’s _much_ more to pattern matching in Scala.
416441
Patterns can be nested, results of patterns can be bound, and pattern matching can even be user-defined.
417442
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:
461486
{% tab 'Scala 2' for=while_1 %}
462487

463488
```scala
464-
while ( x >= 0 ) { x = f(x) }
489+
while (x >= 0) { x = f(x) }
465490
```
466491

467492
{% endtab %}
@@ -471,12 +496,11 @@ while ( x >= 0 ) { x = f(x) }
471496
```scala
472497
while x >= 0 do x = f(x)
473498
```
499+
Scala 3 still supports the Scala 2 syntax for the sake of compatibility.
474500

475501
{% endtab %}
476502
{% endtabs %}
477503

478-
Scala 3 still supports the Scala 2 syntax for the sake of compatibility.
479-
480504
The `while` loop multiline syntax looks like this:
481505

482506
{% tabs while_2 class=tabs-scala-version %}

0 commit comments

Comments
 (0)