20 template<
typename L,
typename R>
23 using Either_t = std::variant<L, R>;
26 enum { LeftVal, RightVal };
28 static_assert (!std::is_same<L, R>::value,
"Types cannot be the same.");
41 : This_ { std::move (r) }
57 return This_.index () == LeftVal;
62 return This_.index () == RightVal;
68 throw std::runtime_error {
"Tried accessing Left for a Right Either" };
69 return std::get<L> (This_);
75 throw std::runtime_error {
"Tried accessing Right for a Left Either" };
76 return std::get<R> (This_);
100 return std::move (This_);
125 template<
typename LL = L>
128 if constexpr (std::is_same_v<std::decay_t<LL>, std::decay_t<L>>)
131 return Either { L { l } };
136 return Either { std::move (r) };
144 template<
typename RNew>
145 requires (!std::is_convertible_v<RNew, R>)
153 return [] (
const auto& other)
155 static_assert (std::is_convertible<std::decay_t<
decltype (other.GetLeft ())>, L>::value,
156 "Other's Either's Left type is not convertible to this Left type.");
157 return other.IsLeft () ?
165 return e1.This_ == e2.This_;
174 template<
typename L,
typename R,
typename F,
typename = std::invoke_result_t<F>>
182 template<
typename L,
typename R>
190 template<
template<
typename>
class Cont,
typename L,
typename R>
193 std::pair<Cont<L>, Cont<R>> result;
194 for (
const auto& either : eithers)
195 if (either.IsLeft ())
196 result.first.push_back (either.GetLeft ());
198 result.second.push_back (either.GetRight ());
203 template<
typename Left,
typename Right,
typename... Args>
209 template<
typename Left,
typename Right,
typename... Args>
212 return Visit (std::move (either).AsVariant (), std::forward<Args> (args)...);
friend bool operator!=(const Either &e1, const Either &e2)
static Either Left(const LL &l)
static Either Right(const R &r)
Either(Either &&)=default
std::optional< L > MaybeLeft() const
std::variant< L, R > AsVariant() const &
static auto EmbeddingLeft()
Either & operator=(const Either &)=default
static Either Right(R &&r)
const L & GetLeft() const
std::optional< R > MaybeRight() const
friend bool operator==(const Either &e1, const Either &e2)
auto MapLeft(F &&f) const
auto MapRight(F &&f) const
const R & GetRight() const
Either(const Either &)=default
std::variant< L, R > AsVariant() &&
auto Visit(const Either< Left, Right > &either, Args &&... args)
std::pair< Cont< L >, Cont< R > > PartitionEithers(const Cont< Either< L, R > > &eithers)
R RightOr(const Either< L, R > &either, F &&f)