Skip to content

Support for opt-in adaptor for user monad-like types #128

Open

Description

@Bronek

Example semantics of such extension - also available here

#include <expected>
#include <optional>
#include <type_traits>
#include <utility>

template <typename F_>
struct and_then_a {
    F_ _f;
};
template <typename F_>
inline constexpr bool _some_and_then_a = false;
template <typename F_>
inline constexpr bool _some_and_then_a<and_then_a<F_> const&> = true;
template <typename F_>
inline constexpr bool _some_and_then_a<and_then_a<F_>&> = true;
template <typename X>
concept some_and_then_a = _some_and_then_a<X&>;

#define FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)

template <typename T>
struct short_circuit_traits;

template <typename T>
struct short_circuit_traits<T*> {
    static constexpr auto has_value(T* x) -> bool { return x; }
    static constexpr auto get_value(T* x) -> T& { return *x; }
    static constexpr auto nothing() -> T* { return nullptr; }
};

template <typename T>
struct short_circuit_traits<std::optional<T>> {
    static constexpr auto has_value(std::optional<T> const& x) -> bool {
        return x.has_value();
    }
    static constexpr auto get_value(auto&& x) -> auto&& {
        return *std::forward<decltype(x)>(x);
    }
    static constexpr auto nothing() -> std::optional<T> { return std::nullopt; }
};

template <typename T, typename U>
concept similar = std::same_as<T, std::remove_cvref_t<U>>;

template <typename T, typename E>
struct short_circuit_traits<std::expected<T, E>> {
    static constexpr auto has_value(std::expected<T, E> const& x) -> bool {
        return x.has_value();
    }
    static constexpr auto get_value(auto&& x) -> auto&& {
        return FWD(x).value();
    }
    static constexpr auto get_error(similar<std::expected<T, E>> auto &&x) -> auto&& {
        return FWD(x).error();
    }
    static constexpr auto get_error(auto&& x) -> auto&&
     requires (not similar<std::expected<T, E>, decltype(x)>)
    {
        return short_circuit_traits<std::remove_cvref_t<decltype(x)>>::get_error(FWD(x));
    }
};

static constexpr auto operator|(auto&& x_, some_and_then_a auto&& f_) {
    using x_t = std::remove_cvref_t<decltype(x_)>;
    using tr = short_circuit_traits<x_t>;
    using r_t = decltype(FWD(f_)._f(tr::get_value(x_)));
    using r_t_traits = short_circuit_traits<r_t>;

    if (tr::has_value(x_)) {
        return r_t(FWD(f_)._f(tr::get_value(x_)));
    } else {
        if constexpr (requires { r_t_traits::get_error(x_); }) {
            return r_t_traits::get_error(x_);
        } else {
            return r_t_traits::nothing();
        }
    }
}

constexpr inline struct and_then_t {
    constexpr static auto operator()(auto&& f_) {
        return and_then_a{std::forward<decltype(f_)>(f_)};
    }
} and_then;

int main() {
    int x = 5;
    auto px = &x;
    auto r = px | and_then([](int& y) {
                 if (y > 3) {
                     return std::optional<int>(y + 1);
                 } else {
                     return std::optional<int>();
                 }
             });

    return r.has_value();
}

Activity

  1. added
    enhancement
    on Dec 7, 2024
  2. changed the title [-]Support for opt-in adaptor for custom your monad types[/-] [+]Support for opt-in adaptor for user monad-like types[/+] on Dec 7, 2024
  3. added
    release-0.2
    on Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementrelease-0.2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions