A fallible function returns a `result` value, built with ok(payload) on success
or err(message) on failure. The caller recovers a value with `try EXPR else {
… }`: on ok the whole expression is the payload; on err the else block runs —
with the failure message bound to `error` — and its trailing expression supplies
the fallback. It is a plain branch on the result's tag: no exceptions, no hidden
control flow, nothing unwinds. is_ok(r) / is_err(r) classify without unwrapping.
Payloads are any i32-width scalar (int/fixed/bool/entity). The feature is
additive and only kicks in when ok/err/try are used, so untouched programs
compile byte-identically (verified) and the C-free bootstrap fixpoint holds.
Complements panic/assert from #8 (the unrecoverable half). The optional
top-level frame `recover` stays deferred (needs a frame-abort mechanism); the
full tagged-union/any generalization is tracked in #1.
Adds the `try` keyword and ok/err/is_ok/is_err builtins across the compiler,
the vocabulary header, JetBrains + TextMate/VSCode grammars, the docs inventory
and pages, examples/library/recover.ludic, and a regression case.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
892 B
892 B
id: fn-is_err name: is_err category: builtins kind: builtin tokens: is_err sig: is_err(r: result) -> bool tip: True when a result carries a failure. order: 15
Tests a result's tag: true when it was built with err, false when it was built with ok. Use it to detect the failure case explicitly — for example to count or log failures — while try/else handles recovering a value. It is the exact negation of is_ok.
Parameters:
r— the result to test
program Retry {
function step(n: int) -> result {
if n < 3 { return err("not ready") }
return ok(n)
}
test "is_err detects the failure case" {
expect(is_err(step(1)))
expect(not is_err(step(5)))
}
}