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)
@safe
mapError
(
T
E
H
)
(
auto ref Expected!(T, E, H) self
)
if (
is(typeof(op(self.error)))
)

Members

Functions

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

1 {
2     assert(ok(42).mapError!(e => e).value == 42);
3     assert(err("foo").mapError!(e => 42).error == 42);
4     version (D_Exceptions) assert(err("foo").mapError!(e => new Exception(e)).error.msg == "foo");
5 }
6 
7 // remap hook
8 {
9     static struct Hook {}
10     auto res = ok(42).mapError!(e => e, Hook);
11     assert(res == 42);
12     static assert(is(typeof(res) == Expected!(int, string, Hook)));
13 
14     auto res2 = err!int("foo").mapError!(e => "bar", Hook);
15     assert(res2.error == "bar");
16     static assert(is(typeof(res2) == Expected!(int, string, Hook)));
17 }

Meta