prelude.Option

Part of prelude.

enum Option

A value that may not be there.

Plum has no null, so absence is spelled here and the checker makes you handle it.

let Option.is_some (o: Option[T]): Bool

Whether there is a value.

let Option.is_none (o: Option[T]): Bool

Whether there is no value.

let Option.unwrap_or (o: Option[T]) (fallback: T): T

The value, or fallback when there is none.

fallback is evaluated whether or not it is needed. Use unwrap_or_else when computing it costs something.

let Option.unwrap_or_else (o: Option[T]) (f: () -> T): T

The value, or the result of calling f when there is none. The lazy unwrap_or: f runs only when it has to.

let Option.map (o: Option[T]) (f: (T) -> U): Option[U]

Transforms the value if there is one, leaving None alone.

let Option.and_then (o: Option[T]) (f: (T) -> Option[U]): Option[U]

Chains an operation that may itself find nothing.

map with a function returning Option would give Option[Option[U]]; this is the one that keeps it flat.

let Option.ok_or (o: Option[T]) (err: E): Result[T, E]

Turns nothing into an error, so an Option can join a chain of Results.