You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/scala3-book/control-structures.md
+18-8
Original file line number
Diff line number
Diff line change
@@ -369,28 +369,38 @@ val day = i match
369
369
case _ =>"invalid day"// the default, catch-all
370
370
```
371
371
372
-
In this example the variable `i` is tested against the cases shown.
373
-
If it’s between `0` and `6`, `day` is bound to a string that represents one of the days of the week.
374
-
Otherwise, the catch-all case is represented by the `_` character, and `day` is bound to the string, `"invalid day"`.
372
+
In this example, the variable `i` is tested against the cases shown.
373
+
If it’s between `0` and `6`, `day` is bound to the string that represents that day of the week.
374
+
Otherwise, it matches the catch-all case represented by the character, `_`, and `day` is bound to the string, `"invalid day"`.
375
+
376
+
Since the cases are considered in the order they are written, and the first matching case is used, the default case, which matches any value, must come last. Any cases after the catch-all will be warned as unreachable cases.
375
377
376
378
> When writing simple `match` expressions like this, it’s recommended to use the `@switch` annotation on the variable `i`.
377
379
> This annotation provides a compile-time warning if the switch can’t be compiled to a `tableswitch` or `lookupswitch`, which are better for performance.
378
380
379
381
380
382
### Using the default value
381
383
382
-
When you need to access the catch-all, default value in a `match` expression, just provide a variable name on the left side of the `case` statement, and then use that variable name on the right side of the statement as needed:
384
+
When you need to access the catch-all, default value in a `match` expression, just provide a variable name on the left side of the `case` statement instead of `_`, and then use that variable name on the right side of the statement as needed:
383
385
384
386
```scala
385
387
i match
386
388
case0=> println("1")
387
389
case1=> println("2")
388
-
case what => println(s"You gave me: $what")
390
+
case what => println(s"You gave me: $what")
389
391
```
392
+
The name used in the pattern must begin with a lowercase letter.
393
+
A name beginning with an uppercase letter does not introduce a variable, but matches a value in scope:
390
394
391
-
In this example the variable is named `what` to show that it can be given any legal name.
392
-
You can also use `_` as a name to ignore the value.
393
-
395
+
```scala
396
+
valN=42
397
+
i match
398
+
case0=> println("1")
399
+
case1=> println("2")
400
+
caseN=> println("42")
401
+
case n => println(s"You gave me: $n" )
402
+
```
403
+
If `i` is equal to `42`, then `case N` will match, and it will print the string `"42"`. It won't reach the default case.
394
404
395
405
### Handling multiple possible matches on one line
0 commit comments