Skip to content

Commit 7d2efdc

Browse files
committed
[patterns] Clarify terminology around rest elements.
Use "matching rest element" for a rest element with a subpattern and "non-matching rest element" for one without. Fix #2678.
1 parent 7a92641 commit 7d2efdc

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -561,18 +561,22 @@ It is a compile-time error if:
561561
* There is more than one `restPattern` element in the list pattern. *It can
562562
appear anywhere in the list, but there can only be zero or one.*
563563

564-
#### Rest patterns
564+
#### Rest elements
565565

566566
A list pattern may contain a *rest element* which allows matching lists of
567-
arbitrary lengths. If a rest element is present and has a subpattern, all of the
568-
elements not matched by other subpatterns are collected into a new list and that
569-
list is matched against the rest subpattern.
567+
arbitrary lengths. The rest element may also have a subpattern. If a rest
568+
element is present and has a subpattern, all of the elements not matched by
569+
other subpatterns are collected into a new list and that list is matched against
570+
the rest subpattern.
570571

571572
```dart
572573
var [a, b, ...rest, c, d] = [1, 2, 3, 4, 5, 6, 7];
573574
print('$a $b $rest $c $d'); // Prints "1 2 [3, 4, 5] 6 7".
574575
```
575576

577+
We refer to a rest element with a subpattern as a *matching rest element*, and a
578+
rest element with no subpattern as a *non-matching rest element*.
579+
576580
### Map pattern
577581

578582
```
@@ -603,14 +607,14 @@ It is a compile-time error if:
603607

604608
* The `...` element is not the last element in the map pattern.
605609

606-
### Rest patterns
610+
### Rest elements
607611

608-
Like lists, map patterns can also have a rest pattern. However, there's no
609-
well-defined notion of a map "minus" some set of matched entries. Thus, the
610-
rest pattern doesn't allow a subpattern to capture the "remaining" entries.
612+
Like lists, map patterns can also have a rest element. However, there's no
613+
well-defined notion of a map "minus" some set of matched entries. Thus, only a
614+
non-matching rest element is allowed.
611615

612616
Also, there is no ordering to entries in a map, so we only allow the `...` to
613-
appear as the last element. Appearing anywhere else would send a confusing,
617+
appear as the last entry. Appearing anywhere else would send a confusing,
614618
meaningless signal.
615619

616620
In practice, this means that the only purpose of `...` in a map pattern is to
@@ -1638,18 +1642,18 @@ The context type schema for a pattern `p` is:
16381642
16391643
2. Else if `p` has no elements then `E` is `?`.
16401644
1641-
3. Else, infer the type schema from the subpatterns:
1645+
3. Else, infer the type schema from the elements:
16421646
16431647
1. Let `es` be an empty list of type schemas.
16441648
1645-
2. For each subpattern `e` in `p`:
1649+
2. For each element `e` in `p`:
16461650
1647-
1. If `e` is a rest element pattern with a subpattern `s` and the
1651+
1. If `e` is a matching rest element with subpattern `s` and the
16481652
context type schema of `s` is an `Iterable<T>` for some type
16491653
schema `T`, then add `T` to `es`.
16501654
1651-
2. Else if `e` is not a rest element pattern, add the context
1652-
type schema of `e` to `es`.
1655+
2. Else if `e` is not a rest element, add the context type schema
1656+
of `e` to `es`.
16531657
16541658
*Else, `e` is a rest element without an iterable element type, so it
16551659
doesn't contribute to inference.*
@@ -1684,7 +1688,7 @@ The context type schema for a pattern `p` is:
16841688
2. Else if `p` has no entries, then `K` and `V` are `?`.
16851689
16861690
3. Else `K` is `?` and `V` is the greatest lower bound of the context type
1687-
schemas of all value subpatterns. *The rest pattern, if present, doesn't
1691+
schemas of all value subpatterns. *The rest element, if present, doesn't
16881692
contribute to the context type schema.*
16891693
16901694
* **Record**: A record type schema with positional and named fields
@@ -1832,8 +1836,8 @@ To type check a pattern `p` being matched against a value of type `M`:
18321836
18331837
*both `a` and `b` use `num` as their matched value type.*
18341838
1835-
3. If there is a rest element subpattern with a subpattern, type-check its
1836-
subpattern using `List<E>` as the matched value type.
1839+
3. If there is a matching rest element, type-check its subpattern using
1840+
`List<E>` as the matched value type.
18371841
18381842
4. The required type of `p` is `List<E>`.
18391843
@@ -2612,12 +2616,11 @@ To match a pattern `p` against a value `v`:
26122616
26132617
2. Let `l` be the length of the list determined by calling `length` on `v`.
26142618
2615-
3. Let `h` be the number of non-rest element subpatterns preceding the rest
2616-
element if there is one, or the number of subpatterns if there is no
2617-
rest element.
2619+
3. Let `h` be the number of non-rest elements preceding the rest element if
2620+
there is one, or the number of elements if there is no rest element.
26182621
2619-
4. Let `t` be the number of non-rest element subpatterns following the rest
2620-
element if there is one, or zero otherwise.
2622+
4. Let `t` be the number of non-rest elements following the rest element if
2623+
there is one, or zero otherwise.
26212624
26222625
3. If `p` has no rest element and `l` is not equal to `h` then the match
26232626
fails. If `p` has a rest element and `l` is less than `h + t` then the
@@ -2630,16 +2633,15 @@ To match a pattern `p` against a value `v`:
26302633
26312634
2. Match the `i`th element subpattern against `e`.
26322635
2633-
5. If there is a rest element and it has a subpattern:
2636+
5. If there is a matching rest element:
26342637
26352638
1. Let `r` be the result of calling `sublist()` on `v` with arguments
26362639
`h`, and `l - t`.
26372640
26382641
2. Match the rest element subpattern against `r`.
26392642
2640-
*If there is a rest element but it has no subpattern, the unneeded list
2641-
elements are completely skipped and we don't even call `sublist()` to
2642-
access them.*
2643+
*If there is a non-matching rest element, the unneeded list elements are
2644+
completely skipped and we don't even call `sublist()` to access them.*
26432645
26442646
6. Match the tail elements. If `t` is greater than zero, then for `i` from
26452647
`0` to `t - 1`, inclusive:

0 commit comments

Comments
 (0)