Skip to content

Commit

Permalink
Add monadic bind operators, >= and >
Browse files Browse the repository at this point in the history
Signed-off-by: Wolfgang E. Sanyer <[email protected]>
  • Loading branch information
ezzieyguywuf committed Jul 10, 2021
1 parent 68a5f5d commit 8b2a597
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/tl/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,32 @@ class expected : private detail::expected_move_assign_base<T, E>,
typedef E error_type;
typedef unexpected<E> 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<typename F>
auto operator>=(F f) -> std::invoke_result_t<F, 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<typename F>
auto operator>(F f) -> std::invoke_result_t<F>
{
return has_value() ? f() : *this;
}

#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
!defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55)
template <class F> TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) & {
Expand Down

0 comments on commit 8b2a597

Please sign in to comment.