mapOrElse

Maps a Expected<T, E> to U by applying a function to a contained value, or a fallback function to a contained error value.

Both functions has to be of the same return type.

This function can be used to unpack a successful result while handling an error.

template mapOrElse(alias valueOp, alias errorOp)
ref
mapOrElse
(
T
E
H
)
(
auto ref Expected!(T, E, H) self
)
if (
is(typeof(errorOp(forwardError!self))) &&
(
(
is(T == void) &&
is(typeof(valueOp()) == typeof(errorOp(forwardError!self)))
)
||
(
!is(T == void) &&
is(typeof(valueOp(self.value)) == typeof(errorOp(forwardError!self)))
)
)
)

Members

Functions

mapOrElse
auto ref mapOrElse(Expected!(T, E, H) self)

The actual mapOrElse function.

Parameters

valueOp

function called to map Expected value

errorOp

function called to map Expected error

Return Value

A new Expected object containing the result.

Examples

assert(ok(42).mapOrElse!(v => v/2, e => 0) == 21);
assert(ok().mapOrElse!(() => true, e => false));
assert(err!int("foo").mapOrElse!(v => v/2, e => 42) == 42);
assert(!err("foo").mapOrElse!(() => true, e => false));

Meta