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>
31 lines
1.5 KiB
Markdown
31 lines
1.5 KiB
Markdown
---
|
|
id: fn-panic
|
|
name: panic
|
|
category: builtins
|
|
kind: builtin
|
|
tokens: panic
|
|
sig: panic(msg: string)
|
|
tip: Abort with a located error message instead of crashing.
|
|
order: 10
|
|
---
|
|
|
|
Prints <code>file:line: panic: <msg></code> to standard error and aborts the process with exit code 1. It turns "something went wrong" into a clear, located message a non-expert can act on — the opposite of a raw segfault or a silent wrong result. Use it for the unrecoverable case: a corrupt asset, a broken invariant, a branch that should be impossible. The source location is baked in at compile time, so the message always points at the exact call. For a guarded check that only aborts when a condition fails, use <a href="fn-assert"><code>assert</code></a>.
|
|
|
|
Recoverable failures — a missing save, a bad network packet — are a different story: those should be values a game can fall back from rather than a `panic`. That value-carrying path is <a href="kw-try"><code>try</code></a>/<code>else</code> over a <a href="fn-ok"><code>result</code></a>; `panic` covers the unrecoverable programmer-bug half.
|
|
|
|
Parameters:
|
|
- `msg` — a message describing what went wrong (a string)
|
|
|
|
```ludic
|
|
program Demo {
|
|
function pick(options: int, n: int) -> int {
|
|
if n < 0 { panic("choice index is negative") }
|
|
if n >= options { panic("choice index out of range") }
|
|
return n
|
|
}
|
|
entry {
|
|
print(pick(3, 1)) # 1
|
|
print(pick(3, 9)) # aborts: Demo.ludic:6: panic: choice index out of range
|
|
}
|
|
}
|
|
```
|