Skip to content

07 · Pattern Matching

Pattern bindings with is

Use is with a value pattern to test and bind in one condition. Captures are visible only along the successful path, including subsequent condition clauses. Existing is Type and is not Type tests keep their meanings.

On this page

Use is with a value pattern to test and bind in one condition. Captures are visible only along the successful path, including subsequent condition clauses. Existing is Type and is not Type tests keep their meanings.

Complete example

Pattern bindings with is.

is_patterns.zolo
Playground
enum Reading { Value(int), Missing }

fn read(input: Reading) -> int {
    guard input is .Value(value) else { return 0 }
    value
}

let input: Reading = .Value(7)
if input is .Value(value) && value > 0 {
    assert(value == 7)
}
assert(read(input) == 7)
assert(read(Reading.Missing) == 0)
let values = [if input is .Value(value) => value]
let names = #{if input is .Value(value) => [value]: "selected"}
assert(values[0] == 7)
assert(names[7] == "selected")

Rules and behavior

Conditions and error recovery · Documentation

From a checkout of the repository:

zolo examples/features/is_patterns.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br