e87985e879
ci / build (ubuntu) (push) Has been cancelled
ci / unit + fmt (push) Has been cancelled
ci / examples (push) Has been cancelled
ci / goldens + tools (push) Has been cancelled
ci / apps (push) Has been cancelled
ci / selfhost smoke (push) Has been cancelled
ci / macos smoke (push) Has been cancelled
ci / windows smoke (push) Has been cancelled
ci / CI gate (push) Has been cancelled
selfhost-loop / bootstrap determinism (push) Has been cancelled
Desugar `is` to tag/value equality in bootstrap and selfhost so LIR no longer drops hIs as false. Resolve monomorphized Result/Option type names for `?` (_Tag/_Data). Call bux_exit(1) after Unwrap panic messages. Add is_operator example and document follow-up fixes.
70 lines
1.6 KiB
Plaintext
70 lines
1.6 KiB
Plaintext
module Std::Result {
|
|
import Std::Io::{PrintLine};
|
|
|
|
extern func bux_exit(code: int);
|
|
|
|
enum Result<T, E> {
|
|
Ok(T),
|
|
Err(E),
|
|
}
|
|
|
|
func Result_NewOk<T, E>(value: T) -> Result<T, E> {
|
|
let r: Result<T, E> = Result { tag: Result_Ok };
|
|
r.data.Ok_0 = value;
|
|
return r;
|
|
}
|
|
|
|
func Result_NewErr<T, E>(msg: E) -> Result<T, E> {
|
|
let r: Result<T, E> = Result { tag: Result_Err };
|
|
r.data.Err_0 = msg;
|
|
return r;
|
|
}
|
|
|
|
func Result_IsOk<T, E>(r: Result<T, E>) -> bool {
|
|
return r.tag == Result_Ok;
|
|
}
|
|
|
|
func Result_IsErr<T, E>(r: Result<T, E>) -> bool {
|
|
return r.tag == Result_Err;
|
|
}
|
|
|
|
func Result_Unwrap<T, E>(r: Result<T, E>) -> T {
|
|
if r.tag != Result_Ok {
|
|
PrintLine("panic: unwrap on Err");
|
|
bux_exit(1);
|
|
}
|
|
return r.data.Ok_0;
|
|
}
|
|
|
|
func Result_UnwrapOr<T, E>(r: Result<T, E>, fallback: T) -> T {
|
|
if r.tag == Result_Ok {
|
|
return r.data.Ok_0;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
func Result_Expect<T, E>(r: Result<T, E>, msg: String) -> T {
|
|
if r.tag != Result_Ok {
|
|
PrintLine(msg);
|
|
bux_exit(1);
|
|
}
|
|
return r.data.Ok_0;
|
|
}
|
|
|
|
func Result_UnwrapErr<T, E>(r: Result<T, E>) -> E {
|
|
if r.tag != Result_Err {
|
|
PrintLine("panic: unwrap_err on Ok");
|
|
bux_exit(1);
|
|
}
|
|
return r.data.Err_0;
|
|
}
|
|
|
|
func Result_Or<T, E>(r: Result<T, E>, other: Result<T, E>) -> Result<T, E> {
|
|
if r.tag == Result_Ok {
|
|
return r;
|
|
}
|
|
return other;
|
|
}
|
|
|
|
}
|