Implemented · v0.5.0-preview.5

Recoverable errors

Handle recoverable runtime errors and model expected outcomes explicitly.

attempt and otherwise bind recoverable failures. The example handles integer division by zero. Permission and resource failures stay fatal under the runtime policy.

Result[T, E] and Option[T] model explicit alternatives. parse_i64 returns Result[i64, text] and utf8_decode returns Result[text, text]. Handle their variants through exhaustive choose. Distinguish a handled error value from a fatal runtime limit.

advanced

examples/advanced/src/main.zbl · v0.5.0-preview.5 · interpreter, bytecode

ZBL source
shape Account:
    name: text
    balance: i64

task multiply(a: i64, b: i64) gives i64:
    give a * b

task safe_divide(a: i64, b: i64) gives i64:
    attempt:
        give a / b
    otherwise problem:
        show problem.kind
        give 0

fixed account = Account("Ada", 120)
items: List[i64] = []
repeat i in 1..4:
    items = append(items, multiply(i, 2))
counts: Map[text, i64] = {"items": len(items)}
when account.balance > counts["items"]:
    show account.name
otherwise:
    show "empty"
show items[2]
show safe_divide(10, 0)

task test_collections():
    assert({"a": 3}["a"] == [3][0])
Recorded output
Ada
6
DivisionByZero
0