@@ -4,7 +4,7 @@ Author: Bob Nystrom
4
4
5
5
Status: Accepted
6
6
7
- Version 2.19 (see [ CHANGELOG] ( #CHANGELOG ) at end)
7
+ Version 2.20 (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] [ ] .
@@ -882,11 +882,35 @@ are allowed while avoiding confusion about the scope of new variables.*
882
882
883
883
It is a compile-time error if:
884
884
885
- * An identifier in a variable pattern does not resolve to a non-final local
886
- variable. * We could allow assigning to other variables or setters, but it
887
- seems strange to allow assigning to ` foo ` when ` foo ` is an instance field on
888
- the surrounding class with an implicit ` this. ` , but not allowing to assign
889
- to ` this.foo ` explicitly. In the future, we may expand pattern assignment
885
+ * An identifier in a variable pattern does not resolve to an assignable local
886
+ variable or formal parameter. A variable is assignable if it is any of:
887
+
888
+ * Non-final
889
+ * Final and definitely unassigned
890
+ * Late final and not definitely assigned
891
+
892
+ * For example, these are all valid:*
893
+
894
+ ``` dart
895
+ test(int parameter) {
896
+ var notFinal;
897
+ final unassignedFinal;
898
+ late final lateFinal;
899
+
900
+ if (c) lateFinal = 'maybe assigned';
901
+
902
+ (notFinal, unassignedFinal, lateFinal) = ('a', 'b', 'c');
903
+ }
904
+ ```
905
+
906
+ *In other words, if the name resolves to a local variable or parameter and
907
+ could be assigned using a normal assignment expression, it can be used in a
908
+ pattern assignment.*
909
+
910
+ *We could allow assigning to other variables or setters, but it seems
911
+ strange to allow assigning to `foo` when `foo` is an instance field on the
912
+ surrounding class with an implicit `this.`, but not allowing to assign to
913
+ `this.foo` explicitly. In the future, we may expand pattern assignment
890
914
syntax to allow other selector expressions. For now, we restrict assignment
891
915
to local variables, which are also the only kind of variables that can be
892
916
declared by patterns.*
@@ -3193,6 +3217,10 @@ Here is one way it could be broken down into separate pieces:
3193
3217
3194
3218
## Changelog
3195
3219
3220
+ ### 2.20
3221
+
3222
+ - Clarify which variables are valid in pattern assignments.
3223
+
3196
3224
### 2.19
3197
3225
3198
3226
- Specify exhaustiveness checking of switch elements.
0 commit comments