31 template<
typename L,
typename R>
34 using Either_t = std::variant<L, R>;
37 enum { LeftVal, RightVal };
39 static_assert (!std::is_same<L, R>::value,
"Types cannot be the same.");
47 : This_ { std::move (r) }
67 : This_ { std::move (left.Value_) }
72 requires std::is_constructible_v<L, LL&&>
74 : This_ { L { std::move (left.Value_) } }
85 return This_.index () == LeftVal;
90 return This_.index () == RightVal;
96 throw std::runtime_error {
"Tried accessing Left for a Right Either" };
97 return std::get<L> (This_);
103 throw std::runtime_error {
"Tried accessing Right for a Left Either" };
104 return std::get<R> (This_);
128 return std::move (This_);
158 static_assert (std::is_convertible_v<LL, L>,
159 "Other's Either's Left type is not convertible to this Left type.");
160 return other.IsLeft () ?
162 Either { other.GetRight () };
168 return e1.This_ == e2.This_;
177 template<
typename L,
typename R,
typename F,
typename = std::invoke_result_t<F>>
185 template<
typename L,
typename R>
193 template<
template<
typename>
class Cont,
typename L,
typename R>
196 std::pair<Cont<L>, Cont<R>> result;
197 for (
const auto& either : eithers)
198 if (either.IsLeft ())
199 result.first.push_back (either.GetLeft ());
201 result.second.push_back (either.GetRight ());
206 template<
typename Left,
typename Right,
typename... Args>
212 template<
typename Left,
typename Right,
typename... Args>
215 return Visit (std::move (either).AsVariant (), std::forward<Args> (args)...);
friend bool operator!=(const Either &e1, const Either &e2)
Either(Left< void >, const L &l)
Either(Either &&)=default
std::optional< L > MaybeLeft() const
std::variant< L, R > AsVariant() const &
static auto EmbeddingLeft()
Either & operator=(const Either &)=default
const L & GetLeft() const
std::optional< R > MaybeRight() const
Either(Left< LL > &&left)
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 > > Partition(const Cont< Either< L, R > > &eithers)
R RightOr(const Either< L, R > &either, F &&f)