@@ -561,18 +561,22 @@ It is a compile-time error if:
561
561
* There is more than one ` restPattern ` element in the list pattern. * It can
562
562
appear anywhere in the list, but there can only be zero or one.*
563
563
564
- #### Rest patterns
564
+ #### Rest elements
565
565
566
566
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.
570
571
571
572
``` dart
572
573
var [a, b, ...rest, c, d] = [1, 2, 3, 4, 5, 6, 7];
573
574
print('$a $b $rest $c $d'); // Prints "1 2 [3, 4, 5] 6 7".
574
575
```
575
576
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
+
576
580
### Map pattern
577
581
578
582
```
@@ -603,14 +607,14 @@ It is a compile-time error if:
603
607
604
608
* The ` ... ` element is not the last element in the map pattern.
605
609
606
- ### Rest patterns
610
+ ### Rest elements
607
611
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 .
611
615
612
616
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,
614
618
meaningless signal.
615
619
616
620
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:
1638
1642
1639
1643
2. Else if `p` has no elements then `E` is `?`.
1640
1644
1641
- 3. Else, infer the type schema from the subpatterns :
1645
+ 3. Else, infer the type schema from the elements :
1642
1646
1643
1647
1. Let `es` be an empty list of type schemas.
1644
1648
1645
- 2. For each subpattern `e` in `p`:
1649
+ 2. For each element `e` in `p`:
1646
1650
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
1648
1652
context type schema of `s` is an `Iterable<T>` for some type
1649
1653
schema `T`, then add `T` to `es`.
1650
1654
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`.
1653
1657
1654
1658
*Else, `e` is a rest element without an iterable element type, so it
1655
1659
doesn't contribute to inference.*
@@ -1684,7 +1688,7 @@ The context type schema for a pattern `p` is:
1684
1688
2. Else if `p` has no entries, then `K` and `V` are `?`.
1685
1689
1686
1690
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
1688
1692
contribute to the context type schema.*
1689
1693
1690
1694
* **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`:
1832
1836
1833
1837
*both `a` and `b` use `num` as their matched value type.*
1834
1838
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.
1837
1841
1838
1842
4. The required type of `p` is `List<E>`.
1839
1843
@@ -2612,12 +2616,11 @@ To match a pattern `p` against a value `v`:
2612
2616
2613
2617
2. Let `l` be the length of the list determined by calling `length` on `v`.
2614
2618
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.
2618
2621
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.
2621
2624
2622
2625
3. If `p` has no rest element and `l` is not equal to `h` then the match
2623
2626
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`:
2630
2633
2631
2634
2. Match the `i`th element subpattern against `e`.
2632
2635
2633
- 5. If there is a rest element and it has a subpattern :
2636
+ 5. If there is a matching rest element:
2634
2637
2635
2638
1. Let `r` be the result of calling `sublist()` on `v` with arguments
2636
2639
`h`, and `l - t`.
2637
2640
2638
2641
2. Match the rest element subpattern against `r`.
2639
2642
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.*
2643
2645
2644
2646
6. Match the tail elements. If `t` is greater than zero, then for `i` from
2645
2647
`0` to `t - 1`, inclusive:
0 commit comments