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.");
52 return This_.index () == LeftVal;
57 return This_.index () == RightVal;
63 throw std::runtime_error {
"Tried accessing Left for a Right Either" };
64 return std::get<L> (This_);
70 throw std::runtime_error {
"Tried accessing Right for a Left Either" };
71 return std::get<R> (This_);
101 template<
typename RNew>
119 template<
typename...
Vars>
122 return Either { std::visit ([] (
auto&&
arg) {
return L { std::forward<decltype (arg)> (
arg) }; },
var) };
125 template<
typename...
Vars>
133 template<
typename LPrime,
typename = std::enable_if_t<std::is_convertible_v<LPrime, L>>>
141 template<
typename RNew>
149 return [] (
const auto&
other)
151 static_assert (std::is_convertible<std::decay_t<
decltype (
other.GetLeft ())>,
L>
::value,
152 "Other's Either's Left type is not convertible to this Left type.");
153 return other.IsLeft () ?
161 return e1.This_ ==
e2.This_;
170 template<
typename L,
typename R,
typename F,
typename = std::result_of_t<F ()>>
173 return either.IsRight () ?
178 template<
typename L,
typename R>
181 return either.IsRight () ?
186 template<
template<
typename>
class Cont,
typename L,
typename R>
199 template<
typename Left,
typename Right,
typename...
Args>
friend bool operator!=(const Either &e1, const Either &e2)
static Either Right(const R &r)
Either(Either &&)=default
std::optional< L > MaybeLeft() const
static auto EmbeddingLeft()
static Either Left(const L &l)
Either & operator=(const Either &)=default
static Either LeftLift(const Either< std::variant< Vars... >, R > &either)
std::variant< L, R > AsVariant() const
static std::enable_if_t<!std::is_convertible< RNew, R >::value, Either< L, RNew > > Right(const RNew &r)
const L & GetLeft() const
std::optional< R > MaybeRight() const
static Either LeftLift(const std::variant< Vars... > &var)
friend bool operator==(const Either &e1, const Either &e2)
static Either LeftLift(const Either< LPrime, R > &either)
static Either< L, RNew > FromMaybe(const std::optional< RNew > &maybeRight, const L &left)
const R & GetRight() const
Either(const Either &)=default
Container< T > Filter(const Container< T > &c, F f)
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)