@@ -277,22 +277,36 @@ Like parenthesized expressions, parentheses in a pattern let you control
277
277
[ pattern precedence] ( #pattern-precedence ) and insert a lower-precedence
278
278
pattern where a higher precedence one is expected.
279
279
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.
282
284
283
285
<? code-excerpt "language/lib/patterns/pattern_types.dart (parens)"?>
284
286
``` dart
285
287
// ...
288
+ x || y => 'matches true',
286
289
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',
288
293
// ...
289
294
```
290
295
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.
295
297
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.
296
310
297
311
## List
298
312
0 commit comments