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)
@safe
map
(
T
E
H
)
(
auto ref Expected!(T, E, H) self
)
if (
(
is(T == void) &&
is(typeof(op()))
)
||
(
!is(T == void) &&
is(typeof(op(self.value)))
)
)

Members

Functions

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

1 {
2     assert(ok(42).map!(a => a/2).value == 21);
3     assert(ok().map!(() => 42).value == 42);
4     assert(err!int("foo").map!(a => 42).error == "foo");
5     assert(err("foo").map!(() => 42).error == "foo");
6 }
7 
8 // remap hook
9 {
10     static struct Hook {}
11     auto res = ok(42).map!(a => a/2, Hook);
12     assert(res == 21);
13     static assert(is(typeof(res) == Expected!(int, string, Hook)));
14 }

Meta