Skip to content

Commit 72b4336

Browse files
committed
[patterns] Fix examples and clarify implicit named fields in extractors.
Fix #2193.
1 parent 2882a64 commit 72b4336

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Author: Bob Nystrom
44

55
Status: In progress
66

7-
Version 1.6 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 1.7 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -1030,9 +1030,13 @@ destructure fields on the value as that type. This pattern is particularly
10301030
useful for writing code in an algebraic datatype style. For example:
10311031

10321032
```dart
1033-
class Rect {
1033+
class Rect implements Destructure2<double, double> {
10341034
final double width, height;
1035+
10351036
Rect(this.width, this.height);
1037+
1038+
double get field0 => width;
1039+
double get field1 => height;
10361040
}
10371041
10381042
display(Object obj) {
@@ -1061,9 +1065,9 @@ enum Severity {
10611065
10621066
log(Severity severity, String message) {
10631067
switch (severity) {
1064-
case Severity.error(_, prefix):
1068+
case Severity.error(prefix: final prefix):
10651069
print('!! $prefix !! $message'.toUppercase());
1066-
case Severity.warning(_, prefix):
1070+
case Severity.warning(prefix: final prefix):
10671071
print('$prefix: $message');
10681072
}
10691073
}
@@ -1083,6 +1087,21 @@ It is a compile-time error if `extractName` does not refer to a type or enum
10831087
value. It is a compile-time error if a type argument list is present and does
10841088
not match the arity of the type of `extractName`.
10851089

1090+
As with record matchers, a named field without a matcher is implicitly treated
1091+
as containing a variable matcher with the same name as the field. The variable
1092+
is always `final`. The previous example could be written like:
1093+
1094+
```dart
1095+
log(Severity severity, String message) {
1096+
switch (severity) {
1097+
case Severity.error(prefix:):
1098+
print('!! $prefix !! $message'.toUppercase());
1099+
case Severity.warning(prefix:):
1100+
print('$prefix: $message');
1101+
}
1102+
}
1103+
```
1104+
10861105
#### Null-check matcher
10871106

10881107
Similar to the null-assert binder, a null-check matcher provides a nicer syntax
@@ -1567,8 +1586,9 @@ The variables a patterns binds depend on what kind of pattern it is:
15671586
the binders nested inside this create final or assignable variables and
15681587
then introduces those variables.
15691588
1570-
* **Extractor matcher**: May contain type argument patterns and introduces
1571-
all of the variables of its subpatterns.
1589+
* **Extractor matcher**: May contain type argument patterns and introduces all
1590+
of the variables of its subpatterns. A named field with no subpattern
1591+
implicitly defines a `final` variable with the same name as the field.
15721592
15731593
All variables (except for type variables) declared in an instance field pattern
15741594
variable declaration are covariant if the pattern variable declaration is marked
@@ -1873,6 +1893,13 @@ main() {
18731893

18741894
## Changelog
18751895

1896+
### 1.7
1897+
1898+
- Fix object destructuring examples and clarify that extract matchers support
1899+
the named field destructuring shorthand too ([#2193][]).
1900+
1901+
[#2193]: https://github.com/dart-lang/language/issues/2193
1902+
18761903
### 1.6
18771904

18781905
- Change syntax of if-case statement ([#2181][]).

0 commit comments

Comments
 (0)