05 · Control Flow
Conditions with bindings
Comma-separated conditions run lazily from left to right. Successful pattern bindings are visible in subsequent clauses and the success body. A guard introducing bindings must exit on every failure path.
On this page
Comma-separated conditions run lazily from left to right. Successful pattern bindings are visible in subsequent clauses and the success body. A guard introducing bindings must exit on every failure path.
Complete example
Conditions with bindings.
23-condition-chains-and-guard.zolo
// Feature: lazy comma conditions and binding guards.
// Each pattern publishes its bindings only after matching successfully.
enum Entry { Value(int), Missing }
fn positive(entry: Entry) -> int {
guard let .Value(value) = entry, value > 0 else { return 0 }
return value
}
print(positive(Entry.Value(7)))
print(positive(Entry.Value(-1)))
let value = 99
if let .Value(value) = Entry.Value(3), value > 10 {
print(value)
} else {
// Failure sees the outer binding, including failure of a later filter.
print(value)
}
fn next(index: int) -> int? {
if index <= 3 { return index }
return nil
}
let mut index = 1
while let item = next(index), item < 3 {
print(item)
index += 1
}
print(index)
// expected:
// 7
// 0
// 99
// 1
// 2
// 3
Rules and behavior
Conditions and error recovery · Documentation
From a checkout of the repository:
zolo examples/features/04-control-flow/23-condition-chains-and-guard.zolo
See also