Tin
Standard library · Option and Result

On this page

Standard library

Option and Result

There is no null and there are no exceptions. A value that may be missing is an Option; a call that can fail returns a Result, and ? passes a failure up.

Example

let port = parseInt("8080")
print(port.isSome())
print(parseInt("eighty").or(80))
let r: Result<int, string> = parseInt("x").okOr("not a number")
print(r.isErr())
print(r.ok())
true
80
true
None

All 10 symbols

Option.isSome

fn Option.isSome<T>(o: Option<T>): bool

Whether the option holds a value.

Option.isNone

fn Option.isNone<T>(o: Option<T>): bool

Whether the option is empty.

Option.or

fn Option.or<T>(o: Option<T>, fallback: T): T

The value inside, or `fallback` if there is none.

Option.unwrap

fn Option.unwrap<T>(o: Option<T>): T

The value inside. Panics on None, so use it only when None would be a bug.

Option.okOr

fn Option.okOr<T, E>(o: Option<T>, err: E): Result<T, E>

Some(v) becomes Ok(v), None becomes Err(err): lets ? use an Option in a function that returns Result.

Result.isOk

fn Result.isOk<T, E>(r: Result<T, E>): bool

Whether the result is a success.

Result.isErr

fn Result.isErr<T, E>(r: Result<T, E>): bool

Whether the result is a failure.

Result.or

fn Result.or<T, E>(r: Result<T, E>, fallback: T): T

The success value, or `fallback` on failure.

Result.ok

fn Result.ok<T, E>(r: Result<T, E>): Option<T>

Ok(v) becomes Some(v), an Err becomes None: lets ? use a Result in a function that returns Option.

Result.unwrap

fn Result.unwrap<T, E>(r: Result<T, E>): T

The success value. Panics on Err, so use it only when failure would be a bug.

See also