expected icon indicating copy to clipboard operation
expected copied to clipboard

Support changing the error type via `.or_else`

Open Quuxplusone opened this issue 3 years ago • 1 comments

Today I commented on P2505R0 that it should be designed to permit this code:

std::expected<Connection, PortableError> convertError(SSLError e) {
    return std::unexpected(PortableError(e));
}

std::expected<Connection, SSLError> e1 = ~~~;
std::expected<Connection, PortableError> e2 = e1.or_else(convertError);

This doesn't work with tl::expected today. I plan to submit a PR for this feature, but will wait for some of my preliminary PRs to get merged first.

Quuxplusone avatar Jan 02 '22 22:01 Quuxplusone

As a workaround, this is exactly what .map_error does.

Example:

tl::expected<Connection, PortableError> translateError(SSLError e) {
    return tl::make_unexpected(PortableError(e));
}

tl::expected<Connection, PortableError> s = e1.map_error(translateError);

azais-corentin avatar Aug 29 '22 15:08 azais-corentin