@@ -4,7 +4,7 @@ Author: Bob Nystrom
4
4
5
5
Status: Accepted
6
6
7
- Version 2.30 (see [ CHANGELOG] ( #CHANGELOG ) at end)
7
+ Version 2.31 (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] [ ] .
@@ -1344,23 +1344,19 @@ var x = switch (obj) {
1344
1344
A similar ambiguity exists with function expressions in initializer lists, if
1345
1345
the constructor happens to be a factory constructor with ` => ` for its body. We
1346
1346
resolve the ambiguity similarly here: Inside the ` expression ` part of a
1347
- ` guardedPattern ` , a function literal is not allowed, unless it is enclosed in
1348
- grouping operators (parentheses, square brackets, or curly braces). * Therefore,
1349
- if ` => ` is encountered after ` when ` in a guard, the ` => ` is treated as the
1350
- separator between the guard and case body. In the above example, we take the
1351
- first interpretation.*
1352
-
1353
- This rule applies in all contexts where a guard can appear: switch statements,
1354
- switch expressions, if-case statements, and if-case elements. It also applies
1355
- to all function expressions, whether their body is ` => ` followed by an
1356
- expression, or a block delimited by curly braces. * We could restrict this rule
1357
- to guards only in switch expressions where the ambiguity arises, and to function
1358
- literals using ` => ` . But that leads to a syntactic restriction that is
1359
- context-sensitive and harder to learn. Since the rule is unusual enough as it
1360
- is, we apply it as consistently as possible. Note that the related restriction
1361
- on constructor initializers applies regardless of whether the function literal
1362
- uses ` => ` or a block, even though generative constructors can't use ` => ` for
1363
- their body.*
1347
+ ` guardedPattern ` that is part of a ` switchExpression ` , a function literal is not
1348
+ allowed, unless it is enclosed in grouping operators (parentheses, square
1349
+ brackets, or curly braces). * Therefore, if ` => ` is encountered after ` when ` in
1350
+ such a guard, the ` => ` is treated as the separator between the guard and case
1351
+ body. In the above example, we take the first interpretation.*
1352
+
1353
+ This rule applies to all function expressions, whether their body is ` => `
1354
+ followed by an expression, or a block delimited by curly braces. * We could
1355
+ restrict this rule to function literals using ` => ` . But that leads to a
1356
+ syntactic restriction that is harder to implement. Note that the related
1357
+ restriction on constructor initializers applies regardless of whether the
1358
+ function literal uses ` => ` or a block, even though generative constructors can't
1359
+ use ` => ` for their body.*
1364
1360
1365
1361
* The rule is applied unconditionally even if the code after ` => ` is not a valid
1366
1362
body expression, as in:*
@@ -3508,6 +3504,12 @@ Here is one way it could be broken down into separate pieces:
3508
3504
3509
3505
## Changelog
3510
3506
3507
+ ### 2.31
3508
+
3509
+ - Change the technique for resolving the "function expression in guard
3510
+ ambiguity" to match what was implemented (function literals are only
3511
+ prohibited in guards inside switch expressions).
3512
+
3511
3513
### 2.30
3512
3514
3513
3515
- Re-add error for map patterns to have duplicate keys.
0 commit comments