Either represents a computation that produces one of two typed outcomes. By convention, Left carries failure information and Right carries success, allowing errors to remain ordinary values rather than thrown exceptions. section 4.4 section 4.6
enum Either[+E, +A]: case Left(value: E) case Right(value: A)
The two constructors make the alternative outcomes explicit: E describes the error and A describes the successful result. section 4.4
Unlike Option, Either preserves a reason for failure. An empty mean can return Left("mean of empty list!") while a valid input returns Right(xs.sum / xs.length); exceptions can likewise be captured as Left values. section 4.4
Either’s combinators sequence successful computations while propagating the first Left:
- map transforms the Right value and leaves Left unchanged. section 4.6
- flatMap chains functions returning Either, stopping subsequent work after failure. section 4.6
- orElse supplies a fallback after failure, and map2 combines two successful results. section 4.6
- sequence and traverse collect lists of successful results while retaining an error outcome. section 4.6
Parsing and validation
A for-comprehension can parse an age and ticket count with Either.catchNonFatal, then calculate an insurance quote only when both conversions succeed. Constructors for Name and Age similarly return validation errors or validated values, and Person.make combines them with map2. section 4.4 section 4.4
The design replaces hidden, untyped control flow with explicit, referentially transparent results. Higher-order functions centralize propagation and recovery without requiring every call site to handle exceptions manually. section 4.0 section 4.5



