10template<
typename T,
typename E>
11class Either:
public std::expected<T,E>{
13 template<
typename... Args>
14 Either(Args&&... args):
std::expected<T,E>(
std::forward<Args>(args)...){}
25 if constexpr(std::convertible_to<E,std::string_view>){
26 throw std::runtime_error(this->error());
28 throw std::runtime_error(
"No value present!");
41 if constexpr(std::convertible_to<E,std::string_view>){
42 throw std::runtime_error(this->error());
44 throw std::runtime_error(
"No value present!");
56 return std::move(this->value());
57 if constexpr(std::convertible_to<E,std::string_view>){
58 throw std::runtime_error(this->error());
60 throw std::runtime_error(
"No value present!");
74 else throw std::runtime_error(errorMsg);
87 else throw std::runtime_error(errorMsg);
99 return std::move(this->value());
100 else throw std::runtime_error(errorMsg);
112 using U =
typename std::remove_cvref_t<std::invoke_result_t<F, const T&>>::value_type;
113 if(this->has_value())
114 return std::invoke(std::forward<F>(f), this->value());
115 return Either<U,E>(std::unexpected(this->error()));
127 using U =
typename std::remove_cvref_t<std::invoke_result_t<F, T>>::value_type;
128 if(this->has_value())
129 return std::invoke(std::forward<F>(f), std::move(this->value()));
130 return Either<U,E>(std::unexpected(this->error()));
142 using U =
typename std::remove_cvref_t<std::invoke_result_t<F, const T&>>;
143 if(this->has_value())
144 return std::invoke(std::forward<F>(f), this->value());
145 return Either<U,E>(std::unexpected(this->error()));
157 using U =
typename std::remove_cvref_t<std::invoke_result_t<F, T>>;
158 if(this->has_value())
159 return std::invoke(std::forward<F>(f), std::move(this->value()));
160 return Either<U,E>(std::unexpected(this->error()));
170 template<
typename F>
requires std::convertible_to<std::invoke_result_t<F>,
Either<T,E>>
172 if(this->has_value())
174 return Either<T,E>(std::invoke(std::forward<F>(f)));
184 template<
typename F>
requires std::convertible_to<std::invoke_result_t<F>,
Either<T,E>>
186 if(this->has_value())
187 return std::move(*
this);
188 return Either<T,E>(std::invoke(std::forward<F>(f)));
198 if(this->has_value())
199 consumer(std::forward<T>(this->value()));
210 if(this->has_value())
211 consumer(std::forward<T>(this->value()));
213 errorConsumer(std::forward<E>(this->error()));
const T & value_or_throw(const std::string &errorMsg) const &
Get the value of the Either if it has a value, otherwise throw an exception with the provided message...
Definition Either.hpp:71
Either< T, E > or_else(F &&f) &&
If the Either has a value, return it. Otherwise, call the provided function and return its result.
Definition Either.hpp:185
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >::value_type, E > and_then(F &&f) &&
flatMap the value of the Either if it has a value, otherwise return the error
Definition Either.hpp:126
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >, E > transform(F &&f) &
Transform the value of the Either if it has a value, otherwise return the error.
Definition Either.hpp:141
Either(Args &&... args)
Definition Either.hpp:14
Either< T, E > or_else(F &&f) &
If the Either has a value, return it. Otherwise, call the provided function and return its result.
Definition Either.hpp:171
T & value_or_throw(const std::string &errorMsg) &
Get the value of the Either if it has a value, otherwise throw an exception with the provided message...
Definition Either.hpp:84
T & value_or_throw() &
Get the value of the Either if it has a value, otherwise throw an exception using the error value if ...
Definition Either.hpp:38
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >::value_type, E > and_then(F &&f) &
flatMap the value of the Either if it has a value, otherwise return the error
Definition Either.hpp:111
const T & value_or_throw() const &
Get the value of the Either if it has a value, otherwise throw an exception using the error value if ...
Definition Either.hpp:22
T && value_or_throw(const std::string &errorMsg) &&
Get the value of the Either if it has a value, otherwise throw an exception with the provided message...
Definition Either.hpp:97
void if_value(fishnet::util::Consumer< T > auto const &consumer)
If the Either has a value, call the provided function with the value. Otherwise, do nothing.
Definition Either.hpp:197
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >, E > transform(F &&f) &&
Transform the value of the Either if it has a value, otherwise return the error.
Definition Either.hpp:156
T && value_or_throw() &&
Get the value of the Either if it has a value, otherwise throw an exception using the error value if ...
Definition Either.hpp:54
void if_value_or_error(fishnet::util::Consumer< T > auto const &consumer, fishnet::util::Consumer< E > auto const &errorConsumer)
If the Either has a value, call the provided function with the value. Otherwise, call the provided fu...
Definition Either.hpp:209
Definition FunctionalConcepts.hpp:54
Definition FunctionalConcepts.hpp:9
Definition BFSClustering.hpp:5