diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp index 31a5193..b76cbb6 100644 --- a/include/tl/expected.hpp +++ b/include/tl/expected.hpp @@ -1251,6 +1251,32 @@ class expected : private detail::expected_move_assign_base, typedef E error_type; typedef unexpected unexpected_type; + /** + * The "monadic bind" operation. In short: + * + * 1. if `this` contains an Err, return `this` + * 2. otherwise, return result of calling the supplied function `f` + * with the Val contained in `this` as an arguement + * + * Since it returns another `expected` these can be chained. + */ + template + auto operator>=(F f) -> std::invoke_result_t + { + return has_value() ? f(value()) : *this; + } + + /** + * Like `>=` operator, except the function does not take any + * arguments. In other words, discard the results from the previous + * computation. + */ + template + auto operator>(F f) -> std::invoke_result_t + { + return has_value() ? f() : *this; + } + #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) template TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) & {