Skip to content

Commit bf5b00a

Browse files
authored
[patterns] In lists, don't call v[e] if the subpattern is a wildcard. (#2797)
Fix #2671.
1 parent 749404d commit bf5b00a

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

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

Lines changed: 30 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.21 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 2.22 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -2852,9 +2852,20 @@ To match a pattern `p` against a value `v`:
28522852
28532853
5. Match the head elements. For `i` from `0` to `h - 1`, inclusive:
28542854
2855-
1. Extract the element value `e` by calling `v[i]`.
2855+
1. Let `s` be the `i`th element subpattern.
28562856
2857-
2. Match the `i`th element subpattern against `e`.
2857+
2. If `s` is an identifier pattern whose name is `_` then do nothing
2858+
for this element.
2859+
2860+
*Wildcards are useful in list patterns to control the index that
2861+
latter element subpatterns access and to affect the length that the
2862+
pattern checks. When a user does that, we don't want them to pay a
2863+
runtime penalty for accessing list elements that the pattern won't
2864+
use anyway.*
2865+
2866+
3. Else extract the element value `e` by calling `v[i]`.
2867+
2868+
4. Match `s` against `e`.
28582869
28592870
6. If there is a matching rest element:
28602871
@@ -2875,10 +2886,14 @@ To match a pattern `p` against a value `v`:
28752886
7. Match the tail elements. If `t > 0`, then for `i` from `0` to `t - 1`,
28762887
inclusive:
28772888
2878-
1. Extract the element value `e` by calling `v[l - t + i]`.
2889+
1. Let `s` be the subpattern `i` elements after the rest element.
2890+
2891+
2. If `s` is an identifier pattern whose name is `_` then do nothing
2892+
for this element.
28792893
2880-
2. Match the subpattern `i` elements after the rest element against
2881-
`e`.
2894+
3. Else extract the element value `e` by calling `v[l - t + i]`.
2895+
2896+
4. Match `s` against `e`.
28822897
28832898
8. The match succeeds if all subpatterns match.
28842899
@@ -2931,6 +2946,10 @@ To match a pattern `p` against a value `v`:
29312946
29322947
4. For each non-rest entry in `p`, in source order:
29332948
2949+
*Unlike in list patterns, we don't skip wildcard subpatterns. In a map
2950+
pattern, you may want to use a `_` value subpattern to detect whether a
2951+
key is present.*
2952+
29342953
1. Evaluate the key `expression` to `k`.
29352954
29362955
2. Evaluate `v[k]` to `r`.
@@ -3405,6 +3424,11 @@ Here is one way it could be broken down into separate pieces:
34053424
34063425
## Changelog
34073426
3427+
### 2.22
3428+
3429+
- In list patterns, don't call `v[e]` if the corresponding subpattern is a
3430+
wildcard (#2671).
3431+
34083432
### 2.21
34093433
34103434
- Allow object pattern fields to tear off methods (#2561).

0 commit comments

Comments
 (0)