#! @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` 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::is_some: bool { get => this is some; } #! Whether it holds nothing. #! @param this The `maybe`. #! @returns Whether it does. pub val maybe::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::is_some_and(&this, test: F): bool where F: invokable { 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::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::unwrap(&this): T { if const T is closeable { copied::(); } 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::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::expect(&this, why: strview): T { if const T is closeable { copied::(); } 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::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::unwrap_or(&this, fallback: T): T { if const T is closeable { copied::(); } 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::unwrap_or_else(&mut this, make: F): T where F: invokable { 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::take(&mut this): maybe { 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::replace(&mut this, value: T): maybe { var made := move(this); this = maybe:: { 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::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::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::map(&mut this, f: F): maybe where F: invokable { var held := move(this); return maybe:: { 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::and_then(&mut this, f: F): maybe where F: invokable> { 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::filter(&mut this, test: F): maybe where F: invokable { 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::or(&mut this, other: maybe): maybe { 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::or_else(&mut this, make: F): maybe where F: invokable> { 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::ok_or(&mut this, error: E): result { var held := move(this); return result:: { value = move(value) } if held is some(&mut value); return result:: { 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::equals(&this, other: &maybe): bool { if this is some(&mine) { return other is some(&theirs) && equals::(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::hash(&this): u64 { return mix(1, hash::(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::copy_of(other: &maybe): maybe { return maybe:: { some = copy::(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(): 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(data: T): maybe { return maybe:: { some = move(&mut data) }; }