Skip to content

Commit 8dc0a55

Browse files
atsansoneeernstg
andauthored
Clarified parenthetical patterns (#5657)
Fixes #5653 Based on your comment in the issue, I thought you would be the best to review @eernstg . --------- Co-authored-by: Erik Ernst <eernst@google.com>
1 parent 84f7774 commit 8dc0a55

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

examples/language/lib/patterns/pattern_types.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,11 @@ void miscDeclAnalyzedButNotTested() {
264264
var token = switch (result) {
265265
// #docregion parens
266266
// ...
267+
x || y => 'matches true',
267268
x || y && z => 'matches true',
268-
(x || y) && z => 'matches false',
269+
x || (y && z) => 'matches true',
270+
// `x || y && z` is the same thing as `x || (y && z)`.
271+
(x || y) && z => 'matches nothing',
269272
// ...
270273
// #enddocregion parens
271274
_ => throw FormatException('Invalid')

src/content/language/pattern-types.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,36 @@ Like parenthesized expressions, parentheses in a pattern let you control
277277
[pattern precedence](#pattern-precedence) and insert a lower-precedence
278278
pattern where a higher precedence one is expected.
279279

280-
For example, imagine the boolean constants `x`, `y`, and `z` are
281-
equal to `true`, `true`, and `false`, respectively:
280+
For example, imagine the boolean constants `x`, `y`, and `z`
281+
equal `true`, `true`, and `false`, respectively.
282+
Though the following example resembles boolean expression evaulation,
283+
the example matches patterns.
282284

283285
<?code-excerpt "language/lib/patterns/pattern_types.dart (parens)"?>
284286
```dart
285287
// ...
288+
x || y => 'matches true',
286289
x || y && z => 'matches true',
287-
(x || y) && z => 'matches false',
290+
x || (y && z) => 'matches true',
291+
// `x || y && z` is the same thing as `x || (y && z)`.
292+
(x || y) && z => 'matches nothing',
288293
// ...
289294
```
290295

291-
In the first case, the logical-and pattern `y && z` evaluates first because
292-
logical-and patterns have higher precedence than logical-or.
293-
In the next case, the logical-or pattern is parenthesized. It evaluates first,
294-
which results in a different match.
296+
Dart starts matching the pattern from left to right.
295297

298+
1. The first pattern matches `true` as `x` matches `true`.
299+
1. The second pattern matches `true` as `x` matches `true`.
300+
1. The third pattern matches `true` as `x` matches `true`.
301+
1. The fourth pattern `(x || y) && z` has no match.
302+
303+
* The `x` matches `true`, so Dart doesn't try to match `y`.
304+
* Though `(x || y)` matches `true`, `z` doesn't match `true`
305+
* Therefore, pattern `(x || y) && z` doesn't match `true`.
306+
* The subpattern `(x || y)` doesn't match `false`,
307+
so Dart doesn't try to match `z`.
308+
* Therefore, pattern `(x || y) && z` doesn't match `false`.
309+
* As a conclusion, `(x || y) && z` has no match.
296310

297311
## List
298312

0 commit comments

Comments
 (0)