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>
1.2 KiB
1.2 KiB
| id | name | category | kind | tokens | sig | tip | order |
|---|---|---|---|---|---|---|---|
| fn-ok | ok | builtins | builtin | ok | ok(v) -> result | Wrap a success payload in a result — the happy half of try/else. | 12 |
Wraps a success value in a result — the value type a fallible function returns. A result carries either a success payload (this) or a failure message (from err), and a caller recovers it with try/else or classifies it with is_ok / is_err. A function that can fail declares -> result and returns ok(value) on the happy path.
The payload is any i32-width scalar — int, fixed, bool, or entity.
Parameters:
v— the success payload
program Parse {
function to_digit(c: int) -> result {
if c >= 48 and c <= 57 { return ok(c - 48) } # '0'..'9'
return err("not a digit")
}
test "ok carries the parsed value" {
let d = try to_digit(55) else { 0 } # '7' -> 7
expect_eq(d, 7)
}
}