Either
Either
is an abstraction in F-Box that encapsulates a value that can represent one of two cases: a valid result (Right
) or an error/invalid state (Left
). It is commonly used to handle computations that can succeed or fail in a type-safe manner.
The Either
abstraction simplifies error handling and branching logic. By distinguishing between Right
(success) and Left
(failure), it enables composability, error propagation, and concise handling of complex scenarios.
Key Features
- Encapsulation: Wraps values to represent success (
Right
) or failure (Left
). - Error Safety: Propagates errors through computations without manual checks.
- Composability: Provides operators and methods for chaining computations.
- Functional Design: Adheres to functional programming principles with immutability and predictability.
Left and Right
The Either
type has two specific forms:
Left<L>
Represents an Either
with an invalid result or error. Operations like map
, flatMap
, and apply
propagate the Left
instance without executing their logic.
Example
Key Characteristics
- Contains an error or invalid value of type
L
. - Propagates itself through transformations without invoking their logic.
Right<L, R>
Represents an Either
with a valid result. Operations like map
, flatMap
, and apply
execute their logic and produce a new Either
.
Example
Key Characteristics
- Contains a valid value of type
R
. - Executes transformations and returns new
Right
orLeft
instances.
Creating an Either
To create an Either
, use the static method Either.pack
(alias for Either.right
). Alternatively, you can use Either.left
to create a Left
directly.
Example
This demonstrates how to:
- Create a
Right
for valid results. - Create a
Left
for errors or invalid states.
Supported Operators
The Either
abstraction provides the following operators for working with its encapsulated value. Each operator is an alias for a corresponding method.
<$>
: Alias formap
. Applies a transformation function to the value in aRight
.<*>
: Alias forapply
. Combines a function in aRight
with a value in anotherRight
.>>=
: Alias forflatMap
. Chains computations that return newEither
instances.<?>
: Alias fororElse
. Returns the currentEither
if it is aRight
, or a defaultEither
if it is aLeft
.<|>
: Alias forgetOrElse
. Returns the encapsulated value if it is aRight
, or a default value if it is aLeft
.
For detailed usage, see API Methods.
API Methods
Either.pack
Either.pack<R>(value: R): Right<L, R>
Creates a Right
instance containing the given value.
Either.left
Either.left<L>(value: L): Left<L>
Creates a Left
instance encapsulating the given value.
map
map<U>(fn: (value: R) => U): Either<L, U>
Applies a transformation function to the value inside a Right
and returns a new Either
. If the instance is a Left
, it propagates the Left
.
Alias: <$>
apply
apply<A, B>(this: Either<L, (a: A) => B>, boxValue: Either<L, A>): Either<L, B>
Applies a function wrapped in a Right
to a value wrapped in another Right
, returning a new Right
. If either instance is a Left
, it propagates the Left
.
Alias: <*>
flatMap
flatMap<U>(fn: (value: R) => Either<L, U>): Either<L, U>
Applies a function returning an Either
to the value inside a Right
and flattens the result. If the instance is a Left
, it propagates the Left
.
Alias: >>=
orElse
orElse<U>(defaultValue: Either<L, U>): Either<L, U>
Returns the current Either
if it is a Right
, or the given default Either
if it is a Left
.
Alias: <?>
getOrElse
getOrElse(defaultValue: R): R
Returns the value inside a Right
, or the provided default value if it is a Left
.
Alias: <|>
match
match<U>(onLeft: (value: L) => U, onRight: (value: R) => U): U
Matches the current Either
instance against the Left
or Right
cases and applies the corresponding function.
Helper Methods
Either.isLeft
Either.isLeft(value: Either<L, R>): boolean
Checks if a given Either
is a Left
.
Either.isRight
Either.isRight(value: Either<L, R>): boolean
Checks if a given Either
is a Right
.
Either.isEither
Either.isEither(value: any): boolean
Checks if a given value is an Either
.
Use Cases
Handling Success and Failure
Explicitly handle Right
(success) and Left
(failure) cases:
Chaining Computations
Chain multiple computations, stopping at the first failure:
Providing Fallbacks
Provide fallback values or computations:
Why Use Either?
The Either
abstraction offers a robust framework for handling computations that can succeed or fail. By leveraging Right
and Left
to represent valid and invalid results, it enables predictable, composable, and error-safe programming.
Next Steps
Explore other abstractions in F-Box: