Skip to content

Commit a620967

Browse files
Prohibit variable and identifier patterns from being named when or as, to simplify parsing. (#3033)
1 parent bfc1db8 commit a620967

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

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

55
Status: Accepted
66

7-
Version 2.31 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 2.32 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -512,6 +512,10 @@ switch (record) {
512512
}
513513
```
514514

515+
To simplify parsing, the identifier in a variable pattern may not be `when` or
516+
`as`. *This reduces the amount of lookahead needed to correctly parse a guarded
517+
pattern such as `foo when !bar`.*
518+
515519
*There are some restrictions on when `var` and `final` can and can't be used.
516520
They are specified later in the "Pattern context" section.*
517521

@@ -543,6 +547,13 @@ The answer is it's both. Depending on the context where it appears, a bare
543547
identifier pattern may behave like a constant pattern or like a variable
544548
pattern. The section on pattern context below lays out the precise rules.
545549

550+
For consistency with the corresponding rule for variable patterns, the
551+
identifier in an identifier pattern may not be `when` or `as`. *This is not
552+
strictly necessary for parsing, but it should reduce user confusion by making
553+
identifier patterns consistent with variable patterns. Note however that these
554+
identifiers are allowed as parts of a qualifiedName that forms a
555+
constantPattern, e.g. `when.as`.*
556+
546557
#### Wildcards
547558

548559
As with variable patterns, an identifier pattern named `_` is a wildcard that
@@ -3524,6 +3535,11 @@ Here is one way it could be broken down into separate pieces:
35243535
35253536
## Changelog
35263537
3538+
### 2.32
3539+
3540+
- Prohibit variable and identifier patterns from being named `when` or `as`,
3541+
to simplify parsing.
3542+
35273543
### 2.31
35283544
35293545
- Change the technique for resolving the "function expression in guard

0 commit comments

Comments
 (0)