match/case structural pattern matching. The compiler verifies that your match statement is exhaustive — every possible value of the matched type must be covered by some case branch. If you forget a variant, the compiler tells you at build time, not at runtime.
Literal Patterns
Match against specific integer or string literals. Use a wildcardcase _ to cover any value not handled by an earlier branch.
The
case _ wildcard acts as the default branch. The compiler requires it when the matched type has values not covered by the explicit literal cases.OR Patterns
Use| inside a case to match multiple literals in a single branch.
Guard Conditions
Add anif guard to a case to impose an additional condition on the matched value. Guards let you express range checks and predicates without nesting extra if statements inside the branch.
Matching Optional Types
Match onNone explicitly to distinguish between a present and an absent value in an int | None or similar union.
Matching Union Types
Match onint() or str() patterns to dispatch on which member of a union type is present.
Class Pattern Destructuring
Match on a class and bind its fields to named variables in a singlecase clause. Use _ for fields you do not need.
Tuple Patterns
Match on tuples by listing the expected element patterns in parentheses.Nested Patterns with Guards
Combine class destructuring withif guards to express precise conditions. The bound variables from the class pattern are available in the guard expression.
