Skip to content

Commit 39efdc7

Browse files
authored
I did some testing and it looks like the implementations are already (#3278)
doing the right thing and caching trailing list elements across patterns, so we may as well update the proposal to match. This should help ensure we specify it right when it ends up in the spec. Fix #2922.
1 parent e77118e commit 39efdc7

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

accepted/3.0/patterns/feature-specification.md

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

55
Status: Accepted
66

7-
Version 2.32 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 2.33 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -3389,8 +3389,9 @@ To bind invocation keys in a pattern `p` using parent invocation `i`:
33893389

33903390
3. Else `s` is a non-rest element after the rest element:
33913391

3392-
1. Let `e` be `i : ("tail[]", [index])` where `index` is the
3393-
zero-based index of this element subpattern.
3392+
1. Let `e` be `i : ("tail[]", [tailIndex])` where `tailIndex` is
3393+
the number of element subpatterns following this element
3394+
subpattern.
33943395

33953396
*Note the "tail" in the invocation key name. This is to
33963397
distinguish elements after a rest element at some position from
@@ -3409,6 +3410,20 @@ To bind invocation keys in a pattern `p` using parent invocation `i`:
34093410
the third element of the same list. So we use an invocation key
34103411
of "tail[]" for `c` and "[]" for `d`.*
34113412
3413+
*We use `tailIndex` and count backwards from the end so that
3414+
trailing elements can be cached across patterns, as in:*
3415+
3416+
```dart
3417+
switch (list) {
3418+
case [..., var a, var b]: ...
3419+
case [..., var c]: ...
3420+
}
3421+
```
3422+
3423+
*Here, `var b` and `var c` have the same `tailIndex` (0), so
3424+
the second case will use the previously cached list element
3425+
value for `var c`.(
3426+
34123427
2. Bind `e` to the `[]` invocation for `s`.
34133428
34143429
3. Bind invocations in the element subpattern using parent `e`.
@@ -3535,6 +3550,11 @@ Here is one way it could be broken down into separate pieces:
35353550
35363551
## Changelog
35373552
3553+
### 2.33 (after shipping)
3554+
3555+
- Tweak caching of trailing list elements after a rest element. The specified
3556+
behavior now follows the implementations (#2922).
3557+
35383558
### 2.32
35393559
35403560
- Prohibit variable and identifier patterns from being named `when` or `as`,

0 commit comments

Comments
 (0)