Skip to content

Commit 90186a9

Browse files
committed
[patterns] Clarify that switch expressions must always be exhaustive.
Fix #2693.
1 parent 5c808a1 commit 90186a9

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

accepted/future-releases/0546-patterns/feature-specification.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,26 +2371,34 @@ expression are exhaustive or not.
23712371
We don't want to require *all* switches to be exhaustive. The language currently
23722372
does not require switch statements on, say, strings to be exhaustive, and
23732373
requiring that would likely lead to many pointless empty default cases for
2374-
little value. We define an *exhaustive type* to be:
2374+
little value.
2375+
2376+
Exhaustiveness is defined and works for all Dart types, and switch expressions
2377+
must always be exhaustive. But switch statements and switch elements must only
2378+
be exhaustive when the matched value is an *always-exhaustive* type, defined
2379+
as:
23752380

23762381
* `bool`
23772382
* `Null`
23782383
* A enum type
23792384
* A type whose declaration is marked `sealed`
2380-
* `T?` where `T` is exhaustive
2381-
* `FutureOr<T>` for some type `T` that is exhaustive
2382-
* A record type whose fields are all exhaustive types
2383-
2384-
All other types are not exhaustive. Then:
2385+
* `T?` where `T` is always-exhaustive
2386+
* `FutureOr<T>` for some type `T` that is always-exhaustive
2387+
* A record type whose fields are all always-exhaustive types
23852388

2386-
* It is a compile-time error if the cases in a switch expression are not
2387-
exhaustive. *Since an expression must yield a value, the only other option
2388-
is to throw an error and most Dart users prefer to catch those kinds of
2389-
mistakes at compile time.*
2389+
All other types are not always-exhaustive. Then:
23902390

23912391
* It is a compile-time error if the cases in a switch statement or switch
23922392
collection element are not exhaustive and the static type of the matched
2393-
value is an exhaustive type.
2393+
value is an always-exhaustive type. *There is no error if a switch statement
2394+
or switch element is not exhaustive when the type is not an
2395+
always-exhaustive type.*
2396+
2397+
* It is a compile-time error if the cases in a switch expression are not
2398+
exhaustive. *This is an error even if the matched value type is not an
2399+
always-exhaustive type. Since an expression must yield a value, the only
2400+
other option is to throw an error and most Dart users prefer to catch those
2401+
kinds of mistakes at compile time.*
23942402

23952403
[exhaustiveness]: https://github.com/dart-lang/language/blob/master/accepted/future-releases/0546-patterns/exhaustiveness.md
23962404

0 commit comments

Comments
 (0)