@@ -4,7 +4,7 @@ Author: Bob Nystrom
4
4
5
5
Status: Accepted
6
6
7
- Version 2.21 (see [ CHANGELOG] ( #CHANGELOG ) at end)
7
+ Version 2.22 (see [ CHANGELOG] ( #CHANGELOG ) at end)
8
8
9
9
Note: This proposal is broken into a couple of separate documents. See also
10
10
[ records] [ ] and [ exhaustiveness] [ ] .
@@ -2852,9 +2852,20 @@ To match a pattern `p` against a value `v`:
2852
2852
2853
2853
5. Match the head elements. For `i` from `0` to `h - 1`, inclusive:
2854
2854
2855
- 1. Extract the element value `e` by calling `v[i]` .
2855
+ 1. Let `s` be the `i`th element subpattern .
2856
2856
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`.
2858
2869
2859
2870
6. If there is a matching rest element:
2860
2871
@@ -2875,10 +2886,14 @@ To match a pattern `p` against a value `v`:
2875
2886
7. Match the tail elements. If `t > 0`, then for `i` from `0` to `t - 1`,
2876
2887
inclusive:
2877
2888
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.
2879
2893
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`.
2882
2897
2883
2898
8. The match succeeds if all subpatterns match.
2884
2899
@@ -2931,6 +2946,10 @@ To match a pattern `p` against a value `v`:
2931
2946
2932
2947
4. For each non-rest entry in `p`, in source order:
2933
2948
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
+
2934
2953
1. Evaluate the key `expression` to `k`.
2935
2954
2936
2955
2. Evaluate `v[k]` to `r`.
@@ -3405,6 +3424,11 @@ Here is one way it could be broken down into separate pieces:
3405
3424
3406
3425
## Changelog
3407
3426
3427
+ ### 2.22
3428
+
3429
+ - In list patterns, don't call `v[e]` if the corresponding subpattern is a
3430
+ wildcard (#2671).
3431
+
3408
3432
### 2.21
3409
3433
3410
3434
- Allow object pattern fields to tear off methods (#2561).
0 commit comments