core/option.kur
//! Optional values and fallible results.
// Optional and fallible results. Payload multiplicity is structural:
// `Option[T]` and `Result[T, E]` are linear when their payload is linear.
use package as core;
use core::process::trap;
/// A value that is either present as `Some(T)` or absent as `None`.
/// Its multiplicity follows `T`, so an option can carry a linear value.
forall T.
exposed enum Option {
Some(T),
None,
}
/// A successful `Ok(T)` value or a failed `Err(E)` value.
/// Its multiplicity follows its payloads.
forall T, E.
exposed enum Result {
Ok(T),
Err(E),
}
forall T where Copy[T].
impl Option[T] {
/// The value, or `fallback` when there is none. `Copy` only: a linear
/// fallback would be dropped when there is a value.
fn unwrap_or(self, fallback: T) -> T {
match self {
Some(v) => v,
None => fallback,
}
}
/// Whether there is a value. A linear payload is asked with `match &x`
/// instead.
fn is_some(self) -> bool {
match self {
Some(_v) => true,
None => false,
}
}
/// Whether there is no value.
fn is_none(self) -> bool {
match self {
Some(_v) => false,
None => true,
}
}
}
forall T.
impl Option[T] {
/// The value; stops the program (`core::process::trap`) when there is
/// none. For an absence that is a bug, not an outcome to handle.
fn unwrap(self) -> T {
match self {
Some(v) => v,
None => trap(),
}
}
/// `f` of the value, if there is one. `Copy[Env]`: with no value, `f` is
/// dropped uncalled.
forall U, Env where Copy[Env].
fn map(self, f: closure[Env](T) -> U) -> Option[U] {
match self {
Some(v) => Option::Some.(f(v)),
None => Option::None,
}
}
/// `f` of the value, which may itself be absent.
forall U, Env where Copy[Env].
fn and_then(self, f: closure[Env](T) -> Option[U]) -> Option[U] {
match self {
Some(v) => f(v),
None => Option::None,
}
}
/// The value, or `f()` when there is none.
forall Env where Copy[Env].
fn unwrap_or_else(self, f: closure[Env]() -> T) -> T {
match self {
Some(v) => v,
None => f(),
}
}
/// `f` of the value, or `fallback` when there is none. `Copy[U]`: one of
/// `fallback` and `f`'s result is dropped.
forall U, Env where Copy[U], Copy[Env].
fn map_or(self, fallback: U, f: closure[Env](T) -> U) -> U {
match self {
Some(v) => f(v),
None => fallback,
}
}
/// `Ok` of the value, or `Err.(error)` when there is none. `Copy[E]`: with
/// a value, `error` is dropped.
forall E where Copy[E].
fn ok_or(self, error: E) -> Result[T, E] {
match self {
Some(v) => Result::Ok.(v),
None => Result::Err.(error),
}
}
}
forall T, E.
impl Result[T, E] {
/// `f` of the value, keeping an error as it is.
forall U, Env where Copy[Env].
fn map(self, f: closure[Env](T) -> U) -> Result[U, E] {
match self {
Ok(v) => Result::Ok.(f(v)),
Err(e) => Result::Err.(e),
}
}
/// `f` of the error, keeping a value as it is.
forall F, Env where Copy[Env].
fn map_err(self, f: closure[Env](E) -> F) -> Result[T, F] {
match self {
Ok(v) => Result::Ok.(v),
Err(e) => Result::Err.(f(e)),
}
}
/// `f` of the value, which may itself fail.
forall U, Env where Copy[Env].
fn and_then(self, f: closure[Env](T) -> Result[U, E]) -> Result[U, E] {
match self {
Ok(v) => f(v),
Err(e) => Result::Err.(e),
}
}
}
forall T, E where Copy[T], Copy[E].
impl Result[T, E] {
/// Whether this is a value. A linear value or error is asked with
/// `match &r` instead.
fn is_ok(self) -> bool {
match self {
Ok(_v) => true,
Err(_e) => false,
}
}
/// Whether this is an error.
fn is_err(self) -> bool {
match self {
Ok(_v) => false,
Err(_e) => true,
}
}
}
forall T, E where Copy[T], Copy[E].
impl Result[T, E] {
/// The value, or `fallback` on an error. `Copy`: one of the two is
/// dropped.
fn unwrap_or(self, fallback: T) -> T {
match self {
Ok(v) => v,
Err(_e) => fallback,
}
}
}
forall T, E.
impl Result[T, E] {
/// The value, or `f` of the error.
forall Env where Copy[Env].
fn unwrap_or_else(self, f: closure[Env](E) -> T) -> T {
match self {
Ok(v) => v,
Err(e) => f(e),
}
}
}
forall T, E where Copy[T].
impl Result[T, E] {
/// The error as an `Option`. `Copy[T]`: a value is dropped.
fn err(self) -> Option[E] {
match self {
Ok(_v) => Option::None,
Err(e) => Option::Some.(e),
}
}
}
forall T, E where Copy[E].
impl Result[T, E] {
/// The value as an `Option`. `Copy[E]`: an error is dropped.
fn ok(self) -> Option[T] {
match self {
Ok(v) => Option::Some.(v),
Err(_e) => Option::None,
}
}
/// The value; stops the program (`core::process::trap`) on an error,
/// which is dropped (`Copy[E]`). For an error that is a bug.
fn unwrap(self) -> T {
match self {
Ok(v) => v,
Err(_e) => trap(),
}
}
}