Result module

The result of a computation that may contain errors, exceptions, etc.

Similar to the same concept of the same name in Rust. We use the Rust nomenclature for the datatype and its constructors.

public type Result<Ok, Err> = {
  #ok: Ok;
  #err: Err;
};

assertUnwrap

assert that we can unwrap the result; should only be used in tests, not in canister implementations. This will trap.

public func assertUnwrap<Ok, Error>(r: Result<Ok, Error>) : Ok

assertUnwrapAny

public func assertUnwrapAny<Ok>(r: Result<Ok, Any>) : Ok

assertOk

public func assertOk(r: Result<Any, Any>)

assertErr

public func assertErr(r: Result<Any, Any>)

assertErrIs

public func assertErrIs<E>(r: Result<Any, E>, f: E->Bool) : Bool

assertErrAs

public func assertErrAs<E, X>(r:Result<Any, E>, f: E->X) : X

bind

bind operation in result monad.

public func bind<R1, R2, Error>(
  x:Result<R1, Error>,
  y:R1 -> Result<R2, Error>) : Result<R2, Error>

mapOk

map the Ok type/value, leaving any Error type/value unchanged.

public func mapOk<Ok1, Ok2, Error>(
  x: Result<Ok1, Error>,
  y: Ok1 -> Ok2) : Result<Ok2, Error>

fromOption

create a result from an option, including an error value to handle the null case.

public func fromOption<R, E>(x: ?R, err: E) : Result<R, E>

fromSomeMap

map the Ok type/value from the optional value, or else use the given error value.

public func fromSomeMap<R1, R2, E>(x: ?R1, f: R1->R2, err:E) : Result<R2, E>

fromSome

asserts that the option is Some(_) form.

public func fromSome<Ok>(o: ?Ok) : Result<Ok, None>

joinArrayIfOk

a result that consists of an array of Ok results from an array of results, or the first error in the result array, if any.

public func joinArrayIfOk<R, E>(x: [Result<R, E>]) : Result<[R], E>