Skip to content

Commit b8c46f4

Browse files
committed
[patterns] Make map patterns check length like list patterns do.
Also clarify that variables in cases are non-final. Fix #2415. Close #2416.
1 parent fd0d6ff commit b8c46f4

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

working/0546-patterns/patterns-feature-specification.md

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

55
Status: In progress
66

7-
Version 2.0 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 2.2 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -1584,7 +1584,9 @@ The variables a patterns binds depend on what kind of pattern it is:
15841584

15851585
* **Variable** or **cast**: May contain type argument patterns. Introduces a
15861586
variable whose name is the pattern's identifier. The variable is final if
1587-
the surrounding pattern variable declaration has a `final` modifier.
1587+
the surrounding `patternDeclaration` has a `final` modifier. *Variables in
1588+
switch cases don't occur inside pattern variable declarations and thus are
1589+
not final.*
15881590

15891591
The scope where a pattern's variables are declared depends on the construct
15901592
that contains the pattern:
@@ -1825,7 +1827,12 @@ To match a pattern `p` against a value `v`:
18251827
some `K` and `V` determined either by the pattern's explicit type
18261828
arguments or inferred from the matched value type.*
18271829

1828-
2. Otherwise, for each entry in `p`:
1830+
2. If the length of the map determined by calling `length` is not equal to
1831+
the number of subpatterns, then the match fails. *This match failure
1832+
becomes a runtime exception if the map pattern is in a variable
1833+
declaration.*
1834+
1835+
3. Otherwise, for each entry in `p`:
18291836

18301837
1. Evaluate the key `expression` to `k` and call `containsKey()` on the
18311838
value. If this returns `false`, the map does not match.
@@ -1834,7 +1841,7 @@ To match a pattern `p` against a value `v`:
18341841
this entry's value subpattern. If it does not match, the map does
18351842
not match.
18361843

1837-
3. The match succeeds if all entry subpatterns match.
1844+
4. The match succeeds if all entry subpatterns match.
18381845

18391846
*Note that, unlike with lists, a matched map may have additional entries
18401847
that are not checked by the pattern.*
@@ -1951,6 +1958,12 @@ Here is one way it could be broken down into separate pieces:
19511958

19521959
## Changelog
19531960

1961+
### 2.2
1962+
1963+
- Make map patterns check length like list patterns do (#2415).
1964+
1965+
- Clarify that variables in cases are not final (#2416).
1966+
19541967
### 2.1
19551968

19561969
Minor tweaks:

0 commit comments

Comments
 (0)