Skip to content

Commit cb4fe58

Browse files
authored
Merge pull request #2431 from som-snytt/issue/2313-default-case
Clarify case _, case x
2 parents 9d099fa + c52e4b4 commit cb4fe58

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

_overviews/scala3-book/control-structures.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -369,28 +369,38 @@ val day = i match
369369
case _ => "invalid day" // the default, catch-all
370370
```
371371

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.
375377

376378
> When writing simple `match` expressions like this, it’s recommended to use the `@switch` annotation on the variable `i`.
377379
> This annotation provides a compile-time warning if the switch can’t be compiled to a `tableswitch` or `lookupswitch`, which are better for performance.
378380
379381

380382
### Using the default value
381383

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:
383385

384386
```scala
385387
i match
386388
case 0 => println("1")
387389
case 1 => println("2")
388-
case what => println(s"You gave me: $what" )
390+
case what => println(s"You gave me: $what")
389391
```
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:
390394

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+
val N = 42
397+
i match
398+
case 0 => println("1")
399+
case 1 => println("2")
400+
case N => 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.
394404

395405
### Handling multiple possible matches on one line
396406

0 commit comments

Comments
 (0)