Skip to content

Either<A, B> sealed

sealed class Either<A, B> implements Monad<B>

Annotations: @immutable

Represents one of two possible values (a disjoint union).

Instances of Either are a Left or a Right. Eithers are primarily used as an alternative to an Option where a "missing" value is provided rather than a None. It is also useful to return from functions that may fail, providing an indication for the failure reason in the Left value or the success value in the Right value.

Either is right-biased so functions like map, flatMap, etc. operate on the Right value, if present. If the Either has a Left value, the original Either is returned.

Implemented types

Properties

hashCode no setter override

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Implementation
dart
@override
int get hashCode => fold((a) => a.hashCode, (b) => b.hashCode);

isLeft no setter

bool get isLeft

Returns true if this is a Left, otherwise false is returned.

Implementation
dart
bool get isLeft;

isRight no setter

bool get isRight

Returns true if this is a Right, otherwise false is returned.

Implementation
dart
bool get isRight => !isLeft;

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

Methods

ap() override

Either<A, C> ap<C>(Either<A, C Function(B)> f)

Applies the value of f to the value of this instance if both are a Right. If either this or f is a Left, that value is returned, in that order.

Implementation
dart
@override
Either<A, C> ap<C>(Either<A, Function1<B, C>> f) =>
    fold((a) => left<A, C>(a), (b) => f.fold((a) => left<A, C>(a), (f) => right(f(b))));

bimap()

Either<C, D> bimap<C, D>(C Function(A) fa, D Function(B) fb)

Applies the appropriate function fa or fb to the value of this instance depending on if this is a Left or Right.

Implementation
dart
Either<C, D> bimap<C, D>(Function1<A, C> fa, Function1<B, D> fb) =>
    fold((a) => left<C, D>(fa(a)), (b) => right(fb(b)));

contains()

bool contains(B elem)

Returns true if this instance is a Right and the value equals elem.

Implementation
dart
bool contains(B elem) => fold((_) => false, (b) => elem == b);

ensure()

Either<A, B> ensure(bool Function(B) p, A Function() onFailure)

Checks if the value of this Right satisfies the given predicate p. If this is a Left, then this is returned. If this is a Right but the value doesn't pass the test, the result of evaluating onFailure is returned.

Implementation
dart
Either<A, B> ensure(Function1<B, bool> p, Function0<A> onFailure) =>
    fold((_) => this, (b) => p(b) ? this : left(onFailure()));

exists()

bool exists(bool Function(B) p)
Implementation
dart
bool exists(Function1<B, bool> p) => foldLeft(false, (acc, elem) => acc || p(elem));

filterOrElse()

Either<A, B> filterOrElse(bool Function(B) p, A Function() zero)

Checks if the value of this Right satisfies the given predicate p. If this is a Left or if the Right value does not satisfy p, then the value is replaced by the result of evaluating zero.

Implementation
dart
Either<A, B> filterOrElse(Function1<B, bool> p, Function0<A> zero) =>
    fold((_) => left<A, B>(zero()), (b) => p(b) ? this : left(zero()));

flatMap() override

Either<A, C> flatMap<C>(Either<A, C> Function(B) f)

Applies f to this value is this is a Right. If this is a Left, then the original value is returned.

Implementation
dart
@override
Either<A, C> flatMap<C>(Function1<B, Either<A, C>> f);

fold()

C fold<C>(C Function(A) fa, C Function(B) fb)

Applies fa if this is a Left, or fb if this is a Right.

Implementation
dart
C fold<C>(Function1<A, C> fa, Function1<B, C> fb);

foldLeft()

R2 foldLeft<R2>(R2 init, R2 Function(R2, B) op)

Applies op to init and this value if this is a Right. If this is a Left, init is returned.

Implementation
dart
R2 foldLeft<R2>(R2 init, Function2<R2, B, R2> op) => fold((_) => init, (r) => op(init, r));

foldRight()

R2 foldRight<R2>(R2 init, R2 Function(B, R2) op)

Applies op to this value and init if this is a Right. If this is a Left, init is returned.

Implementation
dart
R2 foldRight<R2>(R2 init, Function2<B, R2, R2> op) => fold((_) => init, (r) => op(r, init));

forall()

bool forall(bool Function(B) p)
Implementation
dart
bool forall(Function1<B, bool> p) => foldLeft(true, (acc, elem) => acc && p(elem));

foreach()

void foreach<U>(U Function(B) f)

Applies side effect f if this is a Right.

Implementation
dart
void foreach<U>(Function1<B, U> f) => fold((_) {}, f);

getOrElse()

B getOrElse(B Function() orElse)

Returns thes value if this is a Right, otherwise, the result of evaluating orElse is returned.

Implementation
dart
B getOrElse(Function0<B> orElse) => fold((_) => orElse(), identity);

leftMap()

Either<C, B> leftMap<C>(C Function(A) f)

Returns a new Either by applying f to the value of this instance if it is a Left.

Implementation
dart
Either<C, B> leftMap<C>(Function1<A, C> f) =>
    fold((a) => left<C, B>(f(a)), (b) => right<C, B>(b));

map() override

Either<A, C> map<C>(C Function(B) f)

Returns a new Either by applying f to the value of this instance if it is a Right.

Implementation
dart
@override
Either<A, C> map<C>(Function1<B, C> f);

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

orElse()

Either<A, B> orElse(Either<A, B> Function() or)

If this instance is a Right, this is returned. Otherwise, the result of evaluating orElse is returned.

Implementation
dart
Either<A, B> orElse(Function0<Either<A, B>> or) => fold((a) => or(), (b) => this);

product()

Either<A, Record> product<C>(Either<A, C> other)

Tuples the values of this Either and other if both are instances of Right. Otherwise, the first Left value is returned, this or other in that order.

Implementation
dart
Either<A, (B, C)> product<C>(Either<A, C> other) => (this, other).tupled;

swap()

Either<B, A> swap()

Returns a new Either where the left and right types are swapped.

Implementation
dart
Either<B, A> swap() => fold((a) => right<B, A>(a), (b) => left(b));

toIList()

IList<B> toIList()

Converts this Either to an IList with one element if this instance is a Right or an empty IList if this is a Left.

Implementation
dart
IList<B> toIList() => fold((_) => nil<B>(), (b) => IList.fromDart([b]));

toOption()

Option<B> toOption()

Converts this Either to an Option. If this instance is a Right, the value will be returned as a Some. If this instance is a Left, None will be returned.

Implementation
dart
Option<B> toOption() => fold((_) => none<B>(), Some.new);

toString() override

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Implementation
dart
@override
String toString() => fold((a) => 'Left($a)', (b) => 'Right($b)');

toValidated()

Validated<A, B> toValidated()

Converts this Either to a Validated, either an Invalid or Valid depending on whether this instance is a Left or Right respectively.

Implementation
dart
Validated<A, B> toValidated() => fold((a) => a.invalid(), (b) => b.valid());

Extension Methods

ensureN() extension

Either<E, Record> ensureN(bool Function(T1, T2) p, E Function() onFailure)

Available on Either<A, B>, provided by the EitherTuple2Ops<E, T1, T2> extension

Implementation
dart
Either<E, (T1, T2)> ensureN(
  Function2<T1, T2, bool> p,
  Function0<E> onFailure,
) => ensure(p.tupled, onFailure);

ensureN() extension

Either<E, Record> ensureN(
  bool Function(T1, T2, T3, T4, T5) p,
  E Function() onFailure,
)

Available on Either<A, B>, provided by the EitherTuple5Ops<E, T1, T2, T3, T4, T5> extension

Implementation
dart
Either<E, (T1, T2, T3, T4, T5)> ensureN(
  Function5<T1, T2, T3, T4, T5, bool> p,
  Function0<E> onFailure,
) => ensure(p.tupled, onFailure);

ensureN() extension

Either<E, Record> ensureN(
  bool Function(T1, T2, T3, T4) p,
  E Function() onFailure,
)

Available on Either<A, B>, provided by the EitherTuple4Ops<E, T1, T2, T3, T4> extension

Implementation
dart
Either<E, (T1, T2, T3, T4)> ensureN(
  Function4<T1, T2, T3, T4, bool> p,
  Function0<E> onFailure,
) => ensure(p.tupled, onFailure);

ensureN() extension

Either<E, Record> ensureN(bool Function(T1, T2, T3) p, E Function() onFailure)

Available on Either<A, B>, provided by the EitherTuple3Ops<E, T1, T2, T3> extension

Implementation
dart
Either<E, (T1, T2, T3)> ensureN(
  Function3<T1, T2, T3, bool> p,
  Function0<E> onFailure,
) => ensure(p.tupled, onFailure);

filterOrElseN() extension

Either<E, Record> filterOrElseN(bool Function(T1, T2, T3) p, E Function() zero)

Available on Either<A, B>, provided by the EitherTuple3Ops<E, T1, T2, T3> extension

Implementation
dart
Either<E, (T1, T2, T3)> filterOrElseN(
  Function3<T1, T2, T3, bool> p,
  Function0<E> zero,
) => filterOrElse(p.tupled, zero);

filterOrElseN() extension

Either<E, Record> filterOrElseN(
  bool Function(T1, T2, T3, T4, T5) p,
  E Function() zero,
)

Available on Either<A, B>, provided by the EitherTuple5Ops<E, T1, T2, T3, T4, T5> extension

Implementation
dart
Either<E, (T1, T2, T3, T4, T5)> filterOrElseN(
  Function5<T1, T2, T3, T4, T5, bool> p,
  Function0<E> zero,
) => filterOrElse(p.tupled, zero);

filterOrElseN() extension

Either<E, Record> filterOrElseN(
  bool Function(T1, T2, T3, T4) p,
  E Function() zero,
)

Available on Either<A, B>, provided by the EitherTuple4Ops<E, T1, T2, T3, T4> extension

Implementation
dart
Either<E, (T1, T2, T3, T4)> filterOrElseN(
  Function4<T1, T2, T3, T4, bool> p,
  Function0<E> zero,
) => filterOrElse(p.tupled, zero);

filterOrElseN() extension

Either<E, Record> filterOrElseN(bool Function(T1, T2) p, E Function() zero)

Available on Either<A, B>, provided by the EitherTuple2Ops<E, T1, T2> extension

Implementation
dart
Either<E, (T1, T2)> filterOrElseN(
  Function2<T1, T2, bool> p,
  Function0<E> zero,
) => filterOrElse(p.tupled, zero);

flatMapN() extension

Either<E, T6> flatMapN<T6>(Either<E, T6> Function(T1, T2, T3, T4, T5) f)

Available on Either<A, B>, provided by the EitherTuple5Ops<E, T1, T2, T3, T4, T5> extension

Implementation
dart
Either<E, T6> flatMapN<T6>(Function5<T1, T2, T3, T4, T5, Either<E, T6>> f) => flatMap(f.tupled);

flatMapN() extension

Either<E, T5> flatMapN<T5>(Either<E, T5> Function(T1, T2, T3, T4) f)

Available on Either<A, B>, provided by the EitherTuple4Ops<E, T1, T2, T3, T4> extension

Implementation
dart
Either<E, T5> flatMapN<T5>(Function4<T1, T2, T3, T4, Either<E, T5>> f) => flatMap(f.tupled);

flatMapN() extension

Either<E, T4> flatMapN<T4>(Either<E, T4> Function(T1, T2, T3) f)

Available on Either<A, B>, provided by the EitherTuple3Ops<E, T1, T2, T3> extension

Implementation
dart
Either<E, T4> flatMapN<T4>(Function3<T1, T2, T3, Either<E, T4>> f) => flatMap(f.tupled);

flatMapN() extension

Either<E, T3> flatMapN<T3>(Either<E, T3> Function(T1, T2) f)

Available on Either<A, B>, provided by the EitherTuple2Ops<E, T1, T2> extension

Implementation
dart
Either<E, T3> flatMapN<T3>(Function2<T1, T2, Either<E, T3>> f) => flatMap(f.tupled);

flatten() extension

Either<A, B> flatten()

Extracts the nested Either via fold.

Available on Either<A, B>, provided by the EitherNestedOps<A, B> extension

Implementation
dart
Either<A, B> flatten() => fold((a) => Either.left<A, B>(a), identity);

foldN() extension

T3 foldN<T3>(T3 Function(E) f, T3 Function(T1, T2) g)

Available on Either<A, B>, provided by the EitherTuple2Ops<E, T1, T2> extension

Implementation
dart
T3 foldN<T3>(
  Function1<E, T3> f,
  Function2<T1, T2, T3> g,
) => fold(f, g.tupled);

foldN() extension

T4 foldN<T4>(T4 Function(E) f, T4 Function(T1, T2, T3) g)

Available on Either<A, B>, provided by the EitherTuple3Ops<E, T1, T2, T3> extension

Implementation
dart
T4 foldN<T4>(
  Function1<E, T4> f,
  Function3<T1, T2, T3, T4> g,
) => fold(f, g.tupled);

foldN() extension

T6 foldN<T6>(T6 Function(E) f, T6 Function(T1, T2, T3, T4, T5) g)

Available on Either<A, B>, provided by the EitherTuple5Ops<E, T1, T2, T3, T4, T5> extension

Implementation
dart
T6 foldN<T6>(
  Function1<E, T6> f,
  Function5<T1, T2, T3, T4, T5, T6> g,
) => fold(f, g.tupled);

foldN() extension

T5 foldN<T5>(T5 Function(E) f, T5 Function(T1, T2, T3, T4) g)

Available on Either<A, B>, provided by the EitherTuple4Ops<E, T1, T2, T3, T4> extension

Implementation
dart
T5 foldN<T5>(
  Function1<E, T5> f,
  Function4<T1, T2, T3, T4, T5> g,
) => fold(f, g.tupled);

foreachN() extension

void foreachN(void Function(T1, T2) f)

Available on Either<A, B>, provided by the EitherTuple2Ops<E, T1, T2> extension

Implementation
dart
void foreachN(Function2<T1, T2, void> f) => foreach(f.tupled);

foreachN() extension

void foreachN(void Function(T1, T2, T3) f)

Available on Either<A, B>, provided by the EitherTuple3Ops<E, T1, T2, T3> extension

Implementation
dart
void foreachN(Function3<T1, T2, T3, void> f) => foreach(f.tupled);

foreachN() extension

void foreachN(void Function(T1, T2, T3, T4) f)

Available on Either<A, B>, provided by the EitherTuple4Ops<E, T1, T2, T3, T4> extension

Implementation
dart
void foreachN(Function4<T1, T2, T3, T4, void> f) => foreach(f.tupled);

foreachN() extension

void foreachN(void Function(T1, T2, T3, T4, T5) f)

Available on Either<A, B>, provided by the EitherTuple5Ops<E, T1, T2, T3, T4, T5> extension

Implementation
dart
void foreachN(Function5<T1, T2, T3, T4, T5, void> f) => foreach(f.tupled);

mapN() extension

Either<E, T3> mapN<T3>(T3 Function(T1, T2) f)

Available on Either<A, B>, provided by the EitherTuple2Ops<E, T1, T2> extension

Implementation
dart
Either<E, T3> mapN<T3>(Function2<T1, T2, T3> f) => map(f.tupled);

mapN() extension

Either<E, T6> mapN<T6>(T6 Function(T1, T2, T3, T4, T5) f)

Available on Either<A, B>, provided by the EitherTuple5Ops<E, T1, T2, T3, T4, T5> extension

Implementation
dart
Either<E, T6> mapN<T6>(Function5<T1, T2, T3, T4, T5, T6> f) => map(f.tupled);

mapN() extension

Either<E, T4> mapN<T4>(T4 Function(T1, T2, T3) f)

Available on Either<A, B>, provided by the EitherTuple3Ops<E, T1, T2, T3> extension

Implementation
dart
Either<E, T4> mapN<T4>(Function3<T1, T2, T3, T4> f) => map(f.tupled);

mapN() extension

Either<E, T5> mapN<T5>(T5 Function(T1, T2, T3, T4) f)

Available on Either<A, B>, provided by the EitherTuple4Ops<E, T1, T2, T3, T4> extension

Implementation
dart
Either<E, T5> mapN<T5>(Function4<T1, T2, T3, T4, T5> f) => map(f.tupled);

Operators

operator ==() override

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Implementation
dart
@override
bool operator ==(Object other) =>
    fold((a) => other is Left && a == other.a, (b) => other is Right && b == other.b);

Static Methods

catching()

Either<A, B> catching<A, B>(
  B Function() body,
  A Function(Object, StackTrace) onError,
)

Something of a FP interface between try/catch blocks. The provided body function will be called and if successful, a Right is returned. If the function throws, a Left will be returned holding the result of applying the error and stack trace to the provided onError function.

Implementation
dart
static Either<A, B> catching<A, B>(
  Function0<B> body,
  Function2<Object, StackTrace, A> onError,
) {
  try {
    return right(body());
  } catch (err, stack) {
    return left(onError(err, stack));
  }
}

cond()

Either<A, B> cond<A, B>(
  bool Function() test,
  B Function() ifTrue,
  A Function() ifFalse,
)

Returns the result of ifTrue wrapped in a Right, when the given test evaluates to true or the result of ifFalse wrapped in a Left.

Implementation
dart
static Either<A, B> cond<A, B>(
  Function0<bool> test,
  Function0<B> ifTrue,
  Function0<A> ifFalse,
) => test() ? right(ifTrue()) : left(ifFalse());

left()

Either<A, B> left<A, B>(A a)

Lifts the given value into a Left.

Implementation
dart
static Either<A, B> left<A, B>(A a) => Left<A, B>(a);

pure()

Either<A, B> pure<A, B>(B b)

Lifts the given value into a Right.

Implementation
dart
static Either<A, B> pure<A, B>(B b) => Right(b);
Either<A, B> right<A, B>(B b)

Lifts the given value into a Right.

Implementation
dart
static Either<A, B> right<A, B>(B b) => Right<A, B>(b);