@@ -4,7 +4,7 @@ Author: Bob Nystrom
4
4
5
5
Status: In progress
6
6
7
- Version 1.6 (see [ CHANGELOG] ( #CHANGELOG ) at end)
7
+ Version 1.7 (see [ CHANGELOG] ( #CHANGELOG ) at end)
8
8
9
9
Note: This proposal is broken into a couple of separate documents. See also
10
10
[ records] [ ] and [ exhaustiveness] [ ] .
@@ -1030,9 +1030,13 @@ destructure fields on the value as that type. This pattern is particularly
1030
1030
useful for writing code in an algebraic datatype style. For example:
1031
1031
1032
1032
``` dart
1033
- class Rect {
1033
+ class Rect implements Destructure2<double, double> {
1034
1034
final double width, height;
1035
+
1035
1036
Rect(this.width, this.height);
1037
+
1038
+ double get field0 => width;
1039
+ double get field1 => height;
1036
1040
}
1037
1041
1038
1042
display(Object obj) {
@@ -1061,9 +1065,9 @@ enum Severity {
1061
1065
1062
1066
log(Severity severity, String message) {
1063
1067
switch (severity) {
1064
- case Severity.error(_, prefix):
1068
+ case Severity.error(prefix: final prefix):
1065
1069
print('!! $prefix !! $message'.toUppercase());
1066
- case Severity.warning(_, prefix):
1070
+ case Severity.warning(prefix: final prefix):
1067
1071
print('$prefix: $message');
1068
1072
}
1069
1073
}
@@ -1083,6 +1087,21 @@ It is a compile-time error if `extractName` does not refer to a type or enum
1083
1087
value. It is a compile-time error if a type argument list is present and does
1084
1088
not match the arity of the type of ` extractName ` .
1085
1089
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
+
1086
1105
#### Null-check matcher
1087
1106
1088
1107
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:
1567
1586
the binders nested inside this create final or assignable variables and
1568
1587
then introduces those variables.
1569
1588
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.
1572
1592
1573
1593
All variables (except for type variables) declared in an instance field pattern
1574
1594
variable declaration are covariant if the pattern variable declaration is marked
@@ -1873,6 +1893,13 @@ main() {
1873
1893
1874
1894
## Changelog
1875
1895
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
+
1876
1903
### 1.6
1877
1904
1878
1905
- Change syntax of if-case statement ([ #2181 ] [ ] ).
0 commit comments