Docs/Language

Language

Errors and cleanup

Represent expected failure with Result and Option, then clean up deterministically.

About 12 minVersion: 0.10.1

Option and Result

Option<T> represents Some(value) or None. Result<T, E> represents Ok(value) or Err(error). Match expressions must cover every possible variant.

result.orvi
1fn load_config(path: string) -> Result<string, string> {2    defer print("load finished")3    let contents = fs.read_text(path)?4    return Ok(contents)5}67let result = load_config("orvi.toml")8print(match result {9    Ok(contents) => contents,10    Err(message) => "error: " + message,11})

Propagate with ?

The ? operator unwraps Some and Ok. None or Err is returned from the current compatible function immediately. This keeps the failure path visible without repeated manual matches.

defer and using

defer registers an expression for LIFO cleanup when the current scope exits, including return, error propagation and cancellation. using binds a value and registers its cleanup expression together.

cleanup.orvi
1fn work() -> Result<void, string> {2    using resource = "database" => print("closed", resource)3    defer print("finished")4    fs.write_text("result.txt", "done")?5    return Ok()6}