Errors
Errors work like Zig’s, with two differences: an error can carry data (any struct or enum can be an error type), and a function always lists every error it can return.
import fs
struct NotFound { path: Str}
struct ParseError { line: i64}
struct Config { name: Str}
fn parse(text: Str) -> Config ! ParseError { if text.len() == 0 { return ParseError { line: 1 } } return Config { name: text.trim() }}
fn read_config(path: Str) -> Config ! NotFound | ParseError { let text = fs.read(&path) catch { return NotFound { path } } let config = parse(text) catch { ParseError(p) => return p } return config}
let c = read_config("/etc/hostname") catch { NotFound(n) => Config { name: "missing " + n.path } ParseError(_) => Config { name: "broken" }}print(c.name.len() > 0)fn f() -> T ! A | Blists every error type that can escapef. Returning a value of one of those types returns it as the error.- A call to a fallible function must be handled where it is made:
try f()gives the value, or returns the error to the caller. The caller must list that error too.f() catch fallbackgivesfallbackif the call failed.f() catch |e| { ... }runs the block only if the call failed. The block either ends with the value to use instead, or leaves (return,break,continue,panic). Inside it,match e { ... }must cover every possible error: by type (NotFound(n) => ...), or for an error enum, variant by variant (Failure.Missing(what) => ...). When the call can fail with only one type, its fields can be read directly (e.message); they are read in place, so taking one out needs.clone().f() catch { Pattern => ... }is short for that match: the arms go straight aftercatch, and must cover every error. A plain block (no=>on its first line) is still a block that ignores the error.
- In a script’s top level, in tests, and in a
fn main() ! ..., an error reachingtrystops the program (or fails the test) witherror: Name. - A function’s error set is never inferred: the signature shows exactly how it can fail.
error IoError = NotFound | Deniednames a set of errors, so long lists are written once:fn load(p: &Str) -> Config ! IoError | ParseError. A set can include other sets; it stands for its members wherever an error list is written (functions, closures,fntypes), and members named twice through sets count once.pub errorexports it (store.StoreError). It is not a type: values still have their own error types, andcatcharms name those.
struct Log { lines: Vec<Str>}
fn work(log: &mut Log) { log.lines.push("start") defer log.lines.push("end") log.lines.push("middle")}
let mut log = Log { lines: [] }work(&mut log)print(log.lines.join(" "))start middle enddefer stmtordefer { ... }runs its code when the enclosing block ends (as in Zig, not at the end of the function as in Go), on every way out: normal end,return,break,continue, andtryforwarding an error.- Several defers run in reverse order, interleaved with dropping variables, so a defer can always use what was declared before it.
- The code runs at that moment and sees the current values.
- A defer cannot
return, usetry, orbreak/continueout of itself, and cannot move variables it did not declare. A variable a pending defer uses cannot be moved away before it runs.
errdefer is defer that runs only when the scope is left with an error:
a try that propagates, return SomeError {...}, or return f() where
f failed (checked at run time). It undoes partial work that would
otherwise leak into the caller’s view. It is only allowed in functions that
can fail.