map

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

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

template map(alias op, Hook = Abort)
ref
map
(
T
E
H
)
(
auto ref Expected!(T, E, H) self
)
if (
(
is(T == void) &&
is(typeof(op()))
)
||
(
!is(T == void) &&
is(typeof(op(forwardValue!self)))
)
)

Members

Functions

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

The actual map function.

Parameters

op

function called to map Expected value

Return Value

A new Expected object containing the result.

Examples

{
    assert(ok(42).map!(a => a/2).value == 21);
    assert(ok().map!(() => 42).value == 42);
    assert(err!int("foo").map!(a => 42).error == "foo");
    assert(err("foo").map!(() => 42).error == "foo");
}

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

Meta