consume

Constructs Expected from the result of the provided function.

If the function is nothrow, it just returns it's result using Expected.

If not, then it consumes it's possible Exception using try catch block and constructs Expected in regards of the result.

template consume(alias fun, Hook = Abort)
@safe
consume
(
Args...
)
(
auto ref Args args
)
if (
is(typeof(fun(args)))
)

Examples

auto fn(int v) { if (v == 42) throw new Exception("don't panic"); return v; }

assert(consume!fn(1) == 1);
assert(consume!fn(42).error.msg == "don't panic");

Meta