Skip to content

Commit c87d200

Browse files
authored
[patterns] Clarify which variables are valid in pattern assignments. (#2686)
This might be too specific. The real intent here is just that if it's a local variable or parameter and you can assign to it outside of a pattern, then you can assign to it in one too. If I correctly captured those rules, great. If not, let me know and I'll tweak it.
1 parent b7edc0b commit c87d200

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

accepted/future-releases/0546-patterns/feature-specification.md

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

55
Status: Accepted
66

7-
Version 2.19 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 2.20 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -882,11 +882,35 @@ are allowed while avoiding confusion about the scope of new variables.*
882882

883883
It is a compile-time error if:
884884

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
890914
syntax to allow other selector expressions. For now, we restrict assignment
891915
to local variables, which are also the only kind of variables that can be
892916
declared by patterns.*
@@ -3193,6 +3217,10 @@ Here is one way it could be broken down into separate pieces:
31933217
31943218
## Changelog
31953219
3220+
### 2.20
3221+
3222+
- Clarify which variables are valid in pattern assignments.
3223+
31963224
### 2.19
31973225
31983226
- Specify exhaustiveness checking of switch elements.

0 commit comments

Comments
 (0)