Files
sakurac/core/results/maybe.sakura
T
2026-12-06 13:52:57 -06:00

287 lines
10 KiB
Plaintext

#! @file maybe.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the properties and methods of `maybe`, and `something`, which wraps
#! a value in one.
#!
#! `maybe`, which holds a value or `nothing`, is among the types the syntax
#! makes, which the compiler declares itself, as it does `i64`: `none` comes
#! first, so a `maybe` left without a value holds nothing. A value converts to
#! a `maybe` of its type wherever one is wanted, and it is read by `is`,
#! `match`, or `else`, as `val x := find(key) else { return; };`. `nothing` is
#! apart from `void`, the value of what gives nothing, so `maybe<void>` tells a
#! value missing from the value of `void`.
#!
#! The methods reading the value, as `unwrap`, move it out of what can be
#! written, as `move` does, so a `maybe` of what closes is left holding
#! nothing, and copy it out of what is only read, which a value that closes
#! refuses. Those making another value of it, as `map`, move it in, as an
#! iterator's adapters do: `as_ref` lends them what one that is only read
#! holds.
module core;
#! Whether it holds a value.
#! @param this The `maybe`.
#! @returns Whether it does.
pub val maybe<T>::is_some: bool { get => this is some; }
#! Whether it holds nothing.
#! @param this The `maybe`.
#! @returns Whether it does.
pub val maybe<T>::is_none: bool { get => this is none; }
#! Whether it holds a value a test passes.
#! @tparam F The test.
#! @param this The `maybe`.
#! @param test The test, given a reference to the value.
#! @returns Whether it holds one that passes.
pub def maybe<T>::is_some_and<F>(&this, test: F): bool
where F: invokable<def(&T): bool>
{
return this is some(&value) && test(value);
}
#! Gives the value it holds, moved out as `move` moves it, so one that closes
#! leaves it holding nothing. It stops the program where it holds nothing.
#! @param this The `maybe`.
#! @returns The value.
pub def maybe<T>::unwrap(&mut this): T {
var held := move(this);
return move(value) if held is some(&mut value);
fault("A `maybe` holding nothing was unwrapped.");
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds nothing.
#! @param this The `maybe`.
#! @returns The value.
pub def maybe<T>::unwrap(&this): T {
if const T is closeable {
copied::<T>();
} else {
return value if this is some(value);
}
fault("A `maybe` holding nothing was unwrapped.");
}
#! Gives the value it holds, moved out as `unwrap` moves it. It stops the
#! program where it holds nothing, saying why.
#! @param this The `maybe`.
#! @param why What went wrong, where it holds nothing.
#! @returns The value.
pub def maybe<T>::expect(&mut this, why: strview): T {
var held := move(this);
return move(value) if held is some(&mut value);
fault(why);
}
#! Gives a copy of the value it holds, which a value that closes refuses. It
#! stops the program where it holds nothing, saying why.
#! @param this The `maybe`.
#! @param why What went wrong, where it holds nothing.
#! @returns The value.
pub def maybe<T>::expect(&this, why: strview): T {
if const T is closeable {
copied::<T>();
} else {
return value if this is some(value);
}
fault(why);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or a fallback
#! where it holds nothing.
#! @param this The `maybe`.
#! @param fallback The value given where it holds nothing.
#! @returns The value, or the fallback.
pub def maybe<T>::unwrap_or(&mut this, fallback: T): T {
var held := move(this);
return move(value) if held is some(&mut value);
return move(&mut fallback);
}
#! Gives a copy of the value it holds, which a value that closes refuses, or a
#! fallback where it holds nothing.
#! @param this The `maybe`.
#! @param fallback The value given where it holds nothing.
#! @returns The value, or the fallback.
pub def maybe<T>::unwrap_or(&this, fallback: T): T {
if const T is closeable {
copied::<T>();
} else {
return value if this is some(value);
}
return move(&mut fallback);
}
#! Gives the value it holds, moved out as `unwrap` moves it, or what a
#! function makes where it holds nothing, which is called only then.
#! @tparam F The function.
#! @param this The `maybe`.
#! @param make The function, making the value where it holds nothing.
#! @returns The value, or what the function made.
pub def maybe<T>::unwrap_or_else<F>(&mut this, make: F): T where F: invokable<def(): T> {
var held := move(this);
return move(value) if held is some(&mut value);
return make();
}
#! Takes what it holds, leaving it holding nothing, whatever the value is.
#! @param this The `maybe`.
#! @returns What it held.
pub def maybe<T>::take(&mut this): maybe<T> {
var made := move(this);
this = nothing {};
return made;
}
#! Puts a value in it, in place of what it held.
#! @param this The `maybe`.
#! @param value The value it holds from now on.
#! @returns What it held before.
pub def maybe<T>::replace(&mut this, value: T): maybe<T> {
var made := move(this);
this = maybe::<T> { some = move(&mut value) };
return made;
}
#! Lends what it holds: a reference to the value, or nothing.
#! @param this The `maybe`.
#! @returns Maybe a reference to the value; otherwise, `nothing`.
pub def maybe<T>::as_ref(&this): maybe<&T> {
return maybe::<&T> { some = value } if this is some(&value);
return nothing {};
}
#! Lends what it holds to be written: a reference to the value, or nothing.
#! @param this The `maybe`.
#! @returns Maybe a reference to the value; otherwise, `nothing`.
pub def maybe<T>::as_mut(&mut this): maybe<&mut T> {
return maybe::<&mut T> { some = value } if this is some(&mut value);
return nothing {};
}
#! Makes a value of the value it holds, which a function is given, moved in.
#! @tparam U What the function makes.
#! @tparam F The function.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns Maybe what the function made; otherwise, `nothing`.
pub def maybe<T>::map<U, F>(&mut this, f: F): maybe<U> where F: invokable<def(T): U> {
var held := move(this);
return maybe::<U> { some = f(move(value)) } if held is some(&mut value);
return nothing {};
}
#! Gives what a function makes of the value it holds, which may be nothing:
#! the next step of work that may find nothing.
#! @tparam U What the function may make.
#! @tparam F The function.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param f The function, called on the value where it holds one.
#! @returns What the function gave; otherwise, `nothing`.
pub def maybe<T>::and_then<U, F>(&mut this, f: F): maybe<U> where F: invokable<def(T): maybe<U>> {
var held := move(this);
return f(move(value)) if held is some(&mut value);
return nothing {};
}
#! Keeps the value it holds where a test passes it.
#! @tparam F The test.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param test The test, given a reference to the value.
#! @returns Maybe the value; otherwise, `nothing` where it fails the test.
pub def maybe<T>::filter<F>(&mut this, test: F): maybe<T> where F: invokable<def(&T): bool> {
var held := move(this);
return held if held is some(&value) && test(value);
return nothing {};
}
#! Gives what it holds where it holds a value, and another `maybe` otherwise.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param other What is given where it holds nothing.
#! @returns It, or the other.
pub def maybe<T>::or(&mut this, other: maybe<T>): maybe<T> {
var held := move(this);
return held if held is some;
return move(&mut other);
}
#! Gives what it holds where it holds a value, and what a function makes
#! otherwise, which is called only then.
#! @tparam F The function.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param make The function, making what is given where it holds nothing.
#! @returns It, or what the function made.
pub def maybe<T>::or_else<F>(&mut this, make: F): maybe<T> where F: invokable<def(): maybe<T>> {
var held := move(this);
return held if held is some;
return make();
}
#! Makes a `result` of it: the value it holds, or an error where it holds
#! nothing.
#! @tparam E The error's type.
#! @param this The `maybe`, moved in as `unwrap` moves it.
#! @param error The error, where it holds nothing.
#! @returns The result.
pub def maybe<T>::ok_or<E>(&mut this, error: E): result<T, E> {
var held := move(this);
return result::<T, E> { value = move(value) } if held is some(&mut value);
return result::<T, E> { error = move(&mut error) };
}
#! Whether it holds what another does: both nothing, or equal values, as `==`
#! compares them.
#! @param this The `maybe`.
#! @param other The other.
#! @returns Whether they are equal.
#! @remarks A `maybe` of a type that is not `equatable` is an error where it is
#! compared.
pub def maybe<T>::equals(&this, other: &maybe<T>): bool {
if this is some(&mine) {
return other is some(&theirs) && equals::<T>(mine, theirs);
}
return other is none;
}
#! Hashes what it holds, so a `maybe` holding nothing hashes apart from one
#! holding a value.
#! @param this The `maybe`.
#! @returns The hash.
#! @remarks A `maybe` of a type that is not `hashable` is an error where it is
#! hashed.
pub def maybe<T>::hash(&this): u64 {
return mix(1, hash::<T>(value)) if this is some(&value);
return 0;
}
#! Builds a copy of a `maybe`, holding a copy of its value, as `copy` makes it.
#! @param other The `maybe` to copy.
#! @returns The copy.
pub def maybe<T>::copy_of(other: &maybe<T>): maybe<T> {
return maybe::<T> { some = copy::<T>(value) } if other is some(&value);
return nothing {};
}
#! Refuses a copy of a value that closes, which a `maybe` or a `result` only
#! read would give of what it holds.
#! @tparam T The type of what it holds.
def copied<T>(): void {
if const T is closeable {
compile_error("A `maybe` or `result` that is only read gives a copy of what it holds, which a value that closes refuses: call it on one that can be written, as a `var`, which moves the value out.");
}
}
#! Wraps a value in a `maybe`, holding it.
#! @tparam T The type of the value.
#! @param data The value to hold.
#! @returns A `maybe` holding the value, as `some`.
pub def something<T>(data: T): maybe<T> {
return maybe::<T> { some = move(&mut data) };
}