Skip to content

Commit 2882a64

Browse files
authored
[patterns] Change if-case statement syntax to a more infix-like form. (#2191)
Instead of: if (case [int x, int y] = json) return Point(x, y); The proposed syntax is now: if (json case [int x, int y]) return Point(x, y); This does *not* define an infix case *expression*. But it is more forward-compatible with that syntax if we decide to do that later. See #2181.
1 parent 6d12ee2 commit 2882a64

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

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

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

55
Status: In progress
66

7-
Version 1.5 (see [CHANGELOG](#CHANGELOG) at end)
7+
Version 1.6 (see [CHANGELOG](#CHANGELOG) at end)
88

99
Note: This proposal is broken into a couple of separate documents. See also
1010
[records][] and [exhaustiveness][].
@@ -577,13 +577,13 @@ form similar to [if-case in Swift][]:
577577
[if-case in swift]: https://useyourloaf.com/blog/swift-if-case-let/
578578

579579
```dart
580-
if (case [int x, int y] = json) return Point(x, y);
580+
if (json case [int x, int y]) return Point(x, y);
581581
```
582582

583583
It may have an else branch as well:
584584

585585
```dart
586-
if (case [int x, int y] = json) {
586+
if (json case [int x, int y]) {
587587
print('Was coordinate array $x,$y');
588588
} else {
589589
throw FormatException('Invalid JSON.');
@@ -593,7 +593,7 @@ if (case [int x, int y] = json) {
593593
The grammar is:
594594

595595
```
596-
ifCaseStatement ::= 'if' '(' 'case' matcher '=' expression ')'
596+
ifCaseStatement ::= 'if' '(' expression 'case' matcher ')'
597597
statement ('else' statement)?
598598
```
599599

@@ -1104,7 +1104,7 @@ is the non-nullable base type of the nullable value being matched:
11041104

11051105
```dart
11061106
String? maybeString = ...
1107-
if (case var s? = maybeString) {
1107+
if (maybeString case var s?) {
11081108
// s has type String here.
11091109
}
11101110
```
@@ -1873,6 +1873,12 @@ main() {
18731873

18741874
## Changelog
18751875

1876+
### 1.6
1877+
1878+
- Change syntax of if-case statement ([#2181][]).
1879+
1880+
[#2181]: https://github.com/dart-lang/language/issues/2181
1881+
18761882
### 1.5
18771883

18781884
- Introduce and clarify type inference.

0 commit comments

Comments
 (0)