@@ -2371,26 +2371,34 @@ expression are exhaustive or not.
2371
2371
We don't want to require * all* switches to be exhaustive. The language currently
2372
2372
does not require switch statements on, say, strings to be exhaustive, and
2373
2373
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:
2375
2380
2376
2381
* ` bool `
2377
2382
* ` Null `
2378
2383
* A enum type
2379
2384
* 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
2385
2388
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:
2390
2390
2391
2391
* It is a compile-time error if the cases in a switch statement or switch
2392
2392
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.*
2394
2402
2395
2403
[ exhaustiveness ] : https://github.com/dart-lang/language/blob/master/accepted/future-releases/0546-patterns/exhaustiveness.md
2396
2404
0 commit comments