Skip to content

Commit 4b709f4

Browse files
committed
[patterns] Link to exhaustiveness proposal from main patterns doc.
1 parent 3d3d227 commit 4b709f4

File tree

1 file changed

+16
-51
lines changed

1 file changed

+16
-51
lines changed

working/0546-patterns/patterns-feature-specification.md

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ Author: Bob Nystrom
44

55
Status: In progress
66

7-
Version 1.3 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 1.4 (see [CHANGELOG](#CHANGELOG) at end)
88

9-
## Summary
9+
Note: This proposal is broken into a couple of separate documents. See also
10+
[records][] and [exhaustiveness][].
11+
12+
[records]: https://github.com/dart-lang/language/blob/master/working/0546-patterns/records-feature-specification.md
1013

11-
This proposal (along with its smaller sister [records proposal][]) covers a
12-
family of closely-related features that address a number of some of the most
13-
highly-voted user requests. It directly addresses:
14+
## Summary
1415

15-
[records proposal]: https://github.com/dart-lang/language/blob/master/working/0546-patterns/records-feature-specification.md
16+
This proposal covers a family of closely-related features that address a number
17+
of some of the most highly-voted user requests. It directly addresses:
1618

1719
* [Multiple return values](https://github.com/dart-lang/language/issues/68) (495 👍, 4th highest)
1820
* [Algebraic datatypes](https://github.com/dart-lang/language/issues/349) (362 👍, 10th highest)
@@ -1549,52 +1551,11 @@ is a key part of maintaining code written in an algebraic datatype style. It's
15491551
the functional equivalent of the error reported when a concrete class fails to
15501552
implement an abstract method.
15511553

1552-
Exhaustiveness checking is *critical* for switch *expressions:*
1553-
1554-
```dart
1555-
int i = switch ("str") {
1556-
case "a" => 1;
1557-
case "oops" => 2;
1558-
};
1559-
```
1560-
1561-
An expression must reliably evaluate to *some* value, unlike statements where
1562-
you can simply do nothing if no case matches. We could throw a runtime error if
1563-
no case matches, but that's generally not useful for users.
1564-
1565-
Exhaustiveness checking is more complex in the presence of pattern matching and
1566-
destructuring:
1567-
1568-
```dart
1569-
bool xor(bool a, bool b) =>
1570-
switch ((a, b)) {
1571-
case (true, true) => false;
1572-
case (true, false) => true;
1573-
case (false, true) => true;
1574-
case (false, false) => false;
1575-
};
1576-
```
1577-
1578-
This is exhaustive, but it's not obvious how this would be determined. A
1579-
related problem is unreachable patterns:
1554+
Exhaustiveness checking over arbitrarily deeply nested record and extractor
1555+
patterns can be complex, so the proposal for that is in a [separate
1556+
document][exhaustiveness].
15801557

1581-
```dart
1582-
switch (obj) {
1583-
case num n: print("number");
1584-
case int i: print("integer");
1585-
}
1586-
```
1587-
1588-
Here, the second case will never match because any value that could match it
1589-
will be caught by the first case. It's not necessary to detect unreachable
1590-
patterns for correctness, but it helps users if we can since the case is dead
1591-
code.
1592-
1593-
A trivial way to ensure all switches are exhaustive is to require a default
1594-
case or case containing only a wildcard pattern (with no guard).
1595-
1596-
**TODO: See if we can detect exhaustive and unreachable cases more precisely.
1597-
See: http://moscova.inria.fr/~maranget/papers/warn/index.html**
1558+
[exhaustiveness]: https://github.com/dart-lang/language/blob/master/working/0546-patterns/exhaustiveness.md
15981559

15991560
## Runtime semantics
16001561

@@ -1826,6 +1787,10 @@ main() {
18261787

18271788
## Changelog
18281789

1790+
### 1.4
1791+
1792+
- Link to [exhaustiveness][] proposal.
1793+
18291794
### 1.3
18301795

18311796
- Avoid unbounded lookahead with switch expression in an expression statement

0 commit comments

Comments
 (0)