Skip to main content
Sifr supports Python 3.10-style 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 wildcard case _ 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 an if 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 on None explicitly to distinguish between a present and an absent value in an int | None or similar union.

Matching Union Types

Match on int() or str() patterns to dispatch on which member of a union type is present.
The compiler checks that all members of the union are covered. If you add a new member to the union later and forget to update a match, the compiler will catch the gap immediately.

Class Pattern Destructuring

Match on a class and bind its fields to named variables in a single case 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 with if guards to express precise conditions. The bound variables from the class pattern are available in the guard expression.
A case with a guard is not considered exhaustive by the compiler, because the guard might not hold. Always include a final case _ or a guard-free catch-all when using guarded patterns.

Pattern Reference