AsException

Hook implementation that behaves like a thrown exception. It throws Exception right when the Expected with error is initialized.

With this, one can easily change the code behavior between Expected idiom or plain Exceptions.

Members

Static functions

onErrorSet
void onErrorSet(E err)

Handler for case when empty error is accessed.

Static variables

enableDefaultConstructor
bool enableDefaultConstructor;

Default constructor for Expected is disabled. Same with the opAssign, so Expected can be only constructed once and not modified afterwards.

Examples

static assert(!isDefaultConstructorEnabled!AsException);
static assert(hasOnErrorSet!(AsException, string));

auto div(int a, int b) {
    if (b != 0) return ok!(string, AsException)(a / b);
    return err!(int, AsException)("oops");
}

assert(div(10, 2) == 5);
assert(collectExceptionMsg!(Unexpected!string)(div(1, 0)) == "oops");

Meta