Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Vavr’s Either<L,R> represents one of two values: a Left or a Right. By convention, use Right for success and Left for failure or error information. Because Vavr’s Either is right-biased, operations such as map and flatMap compose the success path while a Left passes through unchanged.

What Either<L,R> represents

An Either<L,R> is a value that contains either an L or an R. Its concrete cases are Either.Left<L,R> and Either.Right<L,R>. Vavr’s convention is to put failure information in Left and a successful result in Right. The type therefore makes both possible outcomes visible in a method’s return type.

The left and right type parameters can be different: for example, Either<String,Integer> can hold a string error or an integer result. The convention does not force what either side means, but following it makes Vavr’s right-biased operations behave as expected.

How right bias propagates failures

Most fluent operations, including map, flatMap, and filter, operate on the Right value. When the instance is a Left, these operations leave it as a Left rather than running the success-side function. This lets a chain continue through successful results while carrying a failure forward without throwing at each step.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Either<String, Integer> result = Either.right(21)
    .map(i -> i * 2);
// Right(42)

Either<String, Integer> failed = Either.left("bad input")
    .map(i -> i * 2);
// Left("bad input")

In the first chain, map applies the function to 21. In the second, the function is not applied because the value is already a Left; the error value remains available to subsequent handling.

Construct and inspect an Either

Create a value with Either.right(value) or Either.left(error). Use isRight() and isLeft() when a branch check is needed.

  • Either.right(value) creates the success case by convention.
  • Either.left(error) creates the failure case by convention.
  • isRight() and isLeft() report which case the value contains.

The accessors get() and getLeft() are not safe for either case indiscriminately: get() retrieves the right value and throws if the instance is a Left; getLeft() retrieves the left value and throws if it is a Right. Prefer composition and explicit recovery operations over calling an accessor before confirming the case.

When to choose Either

Compared with exceptions

With exception-based control flow, the method signature may not show all failure outcomes a caller must handle. Returning an Either represents the alternative in the type, so callers can compose the success path and decide how to handle a Left. This is useful when failure is an expected result of an operation rather than an exceptional condition that should escape as a thrown exception.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Compared with Try

Use Either when you want the failure information to be part of the result type and want to define what appears on the left. Consider Vavr’s Try when the operation’s failure originates as an exception and exception-oriented handling is the better fit. The choice depends on how the operation reports failure and what callers need to do with it.

Compared with Validation

Either naturally represents one outcome at a time and propagates a single left-side failure through a right-biased chain. If the task is to validate several independent inputs and accumulate multiple validation errors, Vavr’s Validation is the more suitable concept. Check the documentation for the Vavr version in your project for the exact APIs and behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Projections and version-specific APIs

Older Vavr APIs expose LeftProjection and RightProjection. In the Vavr 0.11.0 API, these projections are deprecated: Either is already right-biased, and swap() is recommended when you need to treat the opposite side as active. Since API details differ by version, check the dependency version in your project before using projection methods or copying examples.

Practical guidance

  • Keep the success value on the right and the error information on the left when using the conventional pattern.
  • Use map to transform a successful value and flatMap when the next operation also returns an Either.
  • Choose a meaningful left-side type for the failures callers need to distinguish; it need not be a string.
  • Handle the left case deliberately at an application boundary, rather than repeatedly extracting values with an accessor that can throw.
  • Use APIs and examples that match the Vavr version declared by your project.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.