/* Module: result A type representing either success or failure */ /* Section: Types */ /* Tag: t The result type */ tag t { /* Variant: ok Contains the result value */ ok(T); /* Variant: err Contains the error value */ err(U); } /* Section: Operations */ /* Function: get Get the value out of a successful result Failure: If the result is an error */ fn get(res: t) -> T { alt res { ok(t) { t } err(_) { // FIXME: Serialize the error value // and include it in the fail message (maybe just note it) fail "get called on error result"; } } } /* Function: get_err Get the value out of an error result Failure: If the result is not an error */ fn get_err(res: t) -> U { alt res { err(u) { u } ok(_) { fail "get_error called on ok result"; } } } /* Function: success Returns true if the result is */ pure fn success(res: t) -> bool { alt res { ok(_) { true } err(_) { false } } } /* Function: failure Returns true if the result is */ pure fn failure(res: t) -> bool { !success(res) } pure fn to_either(res: t) -> either::t { alt res { ok(res) { either::right(res) } err(fail_) { either::left(fail_) } } } /* Function: chain Call a function based on a previous result If `res` is then the value is extracted and passed to `op` whereupon `op`s result is returned. if `res` is then it is immediately returned. This function can be used to compose the results of two functions. Example: > let res = chain(read_file(file), { |buf| > ok(parse_buf(buf)) > }) */ fn chain(res: t, op: block(T) -> t) -> t { alt res { ok(t) { op(t) } err(e) { err(e) } } }