Result and Option
import "std/core" (included in import "std") · namespace std.core
Two generic data enums for values that might not be there:
Result<T, E>isOk(value)orErr(error): the outcome of something that can fail, with the reason when it does.Option<T>isSome(value)orNone: a value that may be absent.
Use match to handle each case, or the helper methods to transform and unwrap.
grav
import "std"
fn parsePort(s: string) -> Result<int, string> {
if (!Format.isInt(s)) { return Result.Err("not a number: ${s}") }
let n = Format.toInt(s)
if (n < 1 || n > 65535) { return Result.Err("out of range: ${n}") }
return Result.Ok(n)
}
fn main() {
for (s in ["8080", "http", "99999"]) {
match (parsePort(s)) {
case Ok(port): print("port ${port}")
case Err(msg): print("error: ${msg}")
}
}
print(parsePort("80").map(fn(p: int) => p + 1))
print(parsePort("x").unwrapOr(3000))
print(parsePort("443").ok())
let none: Option<string> = Option.None
print(none.unwrapOr("default"))
print(Option.Some(4).map(fn(n: int) => n * n))
print(Option.Some(4).okOr("missing"))
}port 8080
error: not a number: http
error: out of range: 99999
Ok(81)
3000
Some(443)
default
Some(16)
Ok(4)unwrap()andexpect(message)give the value, or throw aStateErroron anErr/None.maptransforms the value and passes errors through;mapErrtransforms the error;andThenchains another step that returns aResult(orOption).- Type arguments are usually inferred from the arguments and from where the value goes (the declared variable, parameter or return type). When nothing says, write them:
Result.Ok::<int, string>(1),Option.None::<int>().
See Errors: Result and Null safety: Option.
API
enum Result<T, E>
Methods
fn isOk() -> bool— Whether this is Ok.fn isErr() -> bool— Whether this is Err.fn unwrap() -> T— The value; throws StateError (showing the error) when this is an Err.fn expect(message: string) -> T— The value, or throws StateError withmessagewhen this is an Err.fn unwrapOr(fallback: T) -> T— The value, orfallbackwhen this is Err.fn unwrapOrElse(f: fn(E) -> T) -> T— The value, or f(error) when this is Err.fn unwrapErr() -> E— The error; throws StateError when this is an Ok.fn map<U>(f: fn(T) -> U) -> Result<U, E>— Ok(f(value)); errors pass through unchanged.fn mapErr<F>(f: fn(E) -> F) -> Result<T, F>— Err(f(error)); values pass through unchanged.fn andThen<U>(f: fn(T) -> Result<U, E>) -> Result<U, E>— Chains a step that can itself fail.fn ok() -> Option<T>— Some(value) when Ok, None when Err.
Values
Result.Ok(T)Result.Err(E)
enum Option<T>
A value that may be absent.
Methods
fn isSome() -> bool— Whether this holds a value.fn isNone() -> bool— Whether this is None.fn unwrap() -> T— The value; throws StateError when this is None.fn expect(message: string) -> T— The value; StateError withmessagewhen this is None.fn unwrapOr(fallback: T) -> T— The value, orfallbackwhen this is None.fn unwrapOrElse(f: fn() -> T) -> T— The value, or f() when this is None.fn map<U>(f: fn(T) -> U) -> Option<U>— Some(f(value)), or None.fn andThen<U>(f: fn(T) -> Option<U>) -> Option<U>— f(value) (itself an Option), or None.fn filter(keep: fn(T) -> bool) -> Option<T>— This option when it holds a valuekeepaccepts, else None.fn ifSome(f: fn(T))— Calls f with the value, if there is one.fn okOr<E>(error: E) -> Result<T, E>— Ok(value), or Err(error) when None.
Values
Option.Some(T)Option.None