06 · Functions
Pattern function clauses
Consecutive declarations of the same function select a body by parameter patterns and optional guards, in written order. Clauses form one callable and must be exhaustive; guarded clauses alone do not establish coverage. Contracts and public labels must agree.
On this page
Consecutive declarations of the same function select a body by parameter patterns and optional guards, in written order. Clauses form one callable and must be exhaustive; guarded clauses alone do not establish coverage. Contracts and public labels must agree.
Complete example
Pattern function clauses.
24-pattern-function-clauses.zolo
// Consecutive clauses form one function and try patterns in written order.
fn classify(0: int) -> str = "zero"
fn classify(n: int) if n > 0 -> str = "positive"
fn classify(n: int) -> str = "negative"
// Public argument labels are shared; binders remain local to each clause.
fn magnitude(value n: int) if n < 0 -> int = -n
fn magnitude(value n: int) -> int = n
print(classify(0))
print(classify(3))
print(classify(-3))
print(magnitude(value: -4))
// expected: zero
// expected: positive
// expected: negative
// expected: 4
Rules and behavior
Function parameters and lambdas · Documentation
From a checkout of the repository:
zolo examples/features/05-functions/24-pattern-function-clauses.zolo
See also