#pragma once #include #include #include #include #include #include namespace Void::Either { // ── Opt ──────────────────────────────────────────────────────────────────── // Fluent wrapper around std::optional with monadic chaining. // Complements Either: use Opt when a value is simply present or absent, // Either when absent carries a specific error. template class Opt { public: constexpr Opt() = default; explicit constexpr Opt(std::optional o) : inner_{std::move(o)} {} explicit constexpr Opt(T v) : inner_{std::move(v)} {} [[nodiscard]] static constexpr Opt none() { return Opt{std::optional{}}; } [[nodiscard]] static constexpr Opt some(T v) { return Opt{std::move(v)}; } [[nodiscard]] constexpr bool has_value() const noexcept { return inner_.has_value(); } [[nodiscard]] constexpr explicit operator bool() const noexcept { return has_value(); } [[nodiscard]] constexpr const T& operator*() const & { return *inner_; } [[nodiscard]] constexpr T& operator*() & { return *inner_; } [[nodiscard]] constexpr T value_or(T def) const { return inner_.value_or(std::move(def)); } template [[nodiscard]] constexpr auto map(F&& fn) const -> Opt> { using U = std::invoke_result_t; if (!inner_) return Opt{}; return Opt{std::invoke(std::forward(fn), *inner_)}; } template [[nodiscard]] constexpr auto flat_map(F&& fn) const { using Res = std::invoke_result_t; if (!inner_) return Res{}; return std::invoke(std::forward(fn), *inner_); } template [[nodiscard]] constexpr Opt filter(Pred&& pred) const { if (inner_ && std::invoke(std::forward(pred), *inner_)) return *this; return none(); } template [[nodiscard]] constexpr Opt or_else(F&& fn) const { if (inner_) return *this; return std::invoke(std::forward(fn)); } // Convert to Either: Some(v) → Right(v), None → Left(err). template [[nodiscard]] Either ok_or(L err) const { if (inner_) return Either::right(*inner_); return Either::left(std::move(err)); } [[nodiscard]] constexpr std::optional to_optional() const { return inner_; } private: std::optional inner_; }; template [[nodiscard]] constexpr Opt> some(T&& v) { return Opt>::some(std::forward(v)); } template [[nodiscard]] constexpr Opt none() { return Opt::none(); } // ── free optional utilities ─────────────────────────────────────────────────── // Operate on std::optional directly without the Opt wrapper. template [[nodiscard]] constexpr auto opt_transform(std::optional opt, F&& fn) -> std::optional> { if (!opt) return std::nullopt; return std::invoke(std::forward(fn), *std::move(opt)); } template [[nodiscard]] constexpr auto opt_and_then(std::optional opt, F&& fn) { if (!opt) return std::invoke_result_t{}; return std::invoke(std::forward(fn), *std::move(opt)); } template [[nodiscard]] constexpr std::optional opt_filter(std::optional opt, Pred&& pred) { if (opt && std::invoke(std::forward(pred), *opt)) return opt; return std::nullopt; } template [[nodiscard]] const T& opt_expect(const std::optional& opt, const char* msg = "optional is empty") { if (!opt) throw std::runtime_error{msg}; return *opt; } template [[nodiscard]] constexpr std::optional> opt_zip(const std::optional& a, const std::optional& b) { if (a && b) return std::pair{*a, *b}; return std::nullopt; } template [[nodiscard]] constexpr std::optional opt_flatten(std::optional> opt) { if (!opt || !*opt) return std::nullopt; return **opt; } } // namespace Void::Either