Files
Library/include/Void/Functional/Either.hpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

103 lines
3.8 KiB
C++

#pragma once
#include <functional>
#include <type_traits>
#include <utility>
#include <variant>
namespace Void::Either
{
// ── Either<L, R> ──────────────────────────────────────────────────────────────
// Discriminated union: Left<L> for error/secondary value, Right<R> for success.
// Convention: Right is the "happy path".
template <typename L, typename R>
class Either
{
static_assert(!std::is_same_v<L, R>, "Either<L,R>: L and R must be distinct types");
public:
template <typename T>
requires std::is_constructible_v<L, T>
static Either left(T&& v) { return Either{std::in_place_index<0>, std::forward<T>(v)}; }
template <typename T>
requires std::is_constructible_v<R, T>
static Either right(T&& v) { return Either{std::in_place_index<1>, std::forward<T>(v)}; }
[[nodiscard]] bool is_left() const noexcept { return var_.index() == 0; }
[[nodiscard]] bool is_right() const noexcept { return var_.index() == 1; }
[[nodiscard]] L& left_value() { return std::get<0>(var_); }
[[nodiscard]] const L& left_value() const { return std::get<0>(var_); }
[[nodiscard]] R& right_value() { return std::get<1>(var_); }
[[nodiscard]] const R& right_value() const { return std::get<1>(var_); }
template <typename F>
[[nodiscard]] auto map(F&& fn) const & -> Either<L, std::invoke_result_t<F, const R&>>
{
using R2 = std::invoke_result_t<F, const R&>;
if (is_right()) return Either<L, R2>::right(std::invoke(fn, right_value()));
return Either<L, R2>::left(left_value());
}
template <typename F>
[[nodiscard]] auto map(F&& fn) && -> Either<L, std::invoke_result_t<F, R&&>>
{
using R2 = std::invoke_result_t<F, R&&>;
if (is_right()) return Either<L, R2>::right(std::invoke(fn, std::move(right_value())));
return Either<L, R2>::left(std::move(left_value()));
}
template <typename F>
[[nodiscard]] auto map_left(F&& fn) const & -> Either<std::invoke_result_t<F, const L&>, R>
{
using L2 = std::invoke_result_t<F, const L&>;
if (is_left()) return Either<L2, R>::left(std::invoke(fn, left_value()));
return Either<L2, R>::right(right_value());
}
template <typename F>
requires std::invocable<F, const R&>
[[nodiscard]] auto and_then(F&& fn) const & -> std::invoke_result_t<F, const R&>
{
if (is_right()) return std::invoke(fn, right_value());
using Result = std::invoke_result_t<F, const R&>;
return Result::left(left_value());
}
template <typename FL, typename FR>
requires std::invocable<FL, const L&> && std::invocable<FR, const R&>
&& std::is_same_v<std::invoke_result_t<FL, const L&>,
std::invoke_result_t<FR, const R&>>
[[nodiscard]] auto fold(FL&& on_left, FR&& on_right) const
-> std::invoke_result_t<FL, const L&>
{
if (is_left()) return std::invoke(on_left, left_value());
return std::invoke(on_right, right_value());
}
[[nodiscard]] Either<R, L> swap() const
{
if (is_left()) return Either<R, L>::right(left_value());
return Either<R, L>::left(right_value());
}
[[nodiscard]] bool operator==(const Either&) const noexcept = default;
private:
std::variant<L, R> var_;
template <std::size_t I, typename T>
Either(std::in_place_index_t<I> tag, T&& v) : var_{tag, std::forward<T>(v)} {}
};
template <typename L, typename R>
[[nodiscard]] Either<L, R> make_left(L v) { return Either<L, R>::left(std::move(v)); }
template <typename L, typename R>
[[nodiscard]] Either<L, R> make_right(R v) { return Either<L, R>::right(std::move(v)); }
} // namespace Void::Either