The logical-or pattern (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
|) is a composite pattern that matches a value if any of its constituent subpatterns successfully match. It evaluates subpatterns sequentially from left to right and short-circuits, halting evaluation immediately upon the first successful subpattern match.
Pattern Contexts and Refutability
Logical-or patterns are strictly refutable. They are only permitted in refutable contexts where a match failure alters control flow, such asswitch cases, switch expressions, and if case statements.
They cannot be used in irrefutable contexts, such as pattern variable declarations or pattern assignments. The compiler prohibits this because it cannot guarantee at compile time which branch of the logical-or pattern will execute, making deterministic variable initialization impossible.
Evaluation Mechanics
- The incoming value is tested against the left-hand subpattern.
- If the left-hand subpattern matches, the logical-or pattern succeeds, and the right-hand subpattern is completely ignored (short-circuit evaluation).
- If the left-hand subpattern fails, the value is then tested against the right-hand subpattern.
- The logical-or pattern fails if and only if all of its subpatterns fail to match.
Variable Binding Rules
When a logical-or pattern includes variable bindings (extracting values into local variables), Dart enforces strict structural rules to ensure type safety. If any subpattern binds a variable, all subpatterns within the logical-or pattern must bind the exact same set of variables, and those variables must resolve to the exact same types.Precedence and Nesting
The logical-or operator (|) has the lowest precedence among pattern operators. It binds less tightly than the logical-and pattern (&), relational patterns (>, <=, etc.), and cast patterns (as).
To override default precedence, subpatterns must be wrapped in parentheses.
Master Dart with Deep Grasping Methodology!Learn More





