ard/result
Ard represents recoverable errors as values with Result, commonly written with ! syntax:
fn divide(a: Int, b: Int) Int!Str { if b == 0 { Result::err("division by zero") } else { Result::ok(a / b) }}Result is available from the prelude. You can also import ard/result when you want the module namespace explicitly.
Constructors
Section titled “Constructors”Result::ok(val: $T) $T!$E
Section titled “Result::ok(val: $T) $T!$E”Create a successful result.
let result: Int!Str = Result::ok(42)Result::err(err: $E) $T!$E
Section titled “Result::err(err: $E) $T!$E”Create a failed result.
let result: Int!Str = Result::err("failed")Methods
Section titled “Methods”is_ok() Bool
Section titled “is_ok() Bool”Return true when the result is successful.
if result.is_ok() { // success}is_err() Bool
Section titled “is_err() Bool”Return true when the result is an error.
if result.is_err() { // error}expect(message: Str) $T
Section titled “expect(message: Str) $T”Return the success value or fail with message.
let value = result.expect("expected success")or(default: $T) $T
Section titled “or(default: $T) $T”Return the success value or default.
let value = result.or(0)map(with: fn($T) $U) $U!$E
Section titled “map(with: fn($T) $U) $U!$E”Transform the success value while preserving errors.
let result: Int!Str = Result::ok(21)let doubled = result.map(fn(v: Int) Int { v * 2 })map_err(with: fn($E) $F) $T!$F
Section titled “map_err(with: fn($E) $F) $T!$F”Transform the error value while preserving success values.
let result: Int!Str = Result::err("bad")let sized = result.map_err(fn(err: Str) Int { err.size() })and_then(with: fn($T) $U!$E) $U!$E
Section titled “and_then(with: fn($T) $U!$E) $U!$E”Chain another operation that can fail.
fn ensure_even(num: Int) Int!Str { match num % 2 == 0 { true => Result::ok(num), false => Result::err("not even"), }}
let checked = Result::ok(20).and_then(ensure_even)Pattern matching
Section titled “Pattern matching”Use match to handle both cases:
match divide(10, 2) { ok(value) => value, err(message) => 0,}Error propagation with try
Section titled “Error propagation with try”try unwraps an ok value. If the result is an error, the current function returns that error immediately.
fn add_and_divide(a: Int, b: Int, divisor: Int) Int!Str { let sum = a + b let result = try divide(sum, divisor) Result::ok(result + 10)}