mapError

Applies a function to the expected error in an Expected object.

If no error is present, the original value is passed through unchanged, and the function is not called.

template mapError(alias op, Hook = Abort)
ref
mapError
(
T
E
H
)
(
auto ref Expected!(T, E, H) self
)
if (
is(typeof(op(forwardError!self)))
)

Members

Functions

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

The actual mapError function.

Parameters

op

function called to map Expected error

Return Value

A new Expected object containing the result.

Examples

{
    assert(ok(42).mapError!(e => e).value == 42);
    assert(err("foo").mapError!(e => 42).error == 42);
    version (D_Exceptions) assert(err("foo").mapError!(e => new Exception(e)).error.msg == "foo");
}

// remap hook
{
    static struct Hook {}
    auto res = ok(42).mapError!(e => e, Hook);
    assert(res == 42);
    static assert(is(typeof(res) == Expected!(int, string, Hook)));

    auto res2 = err!int("foo").mapError!(e => "bar", Hook);
    assert(res2.error == "bar");
    static assert(is(typeof(res2) == Expected!(int, string, Hook)));
}

Meta