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(auto ref 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

1 static assert(!isDefaultConstructorEnabled!AsException);
2 static assert(hasOnErrorSet!(AsException, string));
3 
4 auto div(int a, int b) {
5     if (b != 0) return ok!(string, AsException)(a / b);
6     return err!(int, AsException)("oops");
7 }
8 
9 assert(div(10, 2) == 5);
10 assert(collectExceptionMsg!(Unexpected!string)(div(1, 0)) == "oops");

Meta