Skip to content

feat: add Either type - #600

Open
ghoullier wants to merge 1 commit into
mainfrom
ghoullier-feat-either
Open

feat: add Either type#600
ghoullier wants to merge 1 commit into
mainfrom
ghoullier-feat-either

Conversation

@ghoullier

Copy link
Copy Markdown
Owner

Summary

Adds an Either<Left, Right> type to the FP library.

What is Either?

Either represents a disjunction — a value that is one of two possible types — without implying error semantics. This complements Result, which specifically models success/failure.

Use Either for general branching logic where both sides are equally valid (e.g., Either<string, number> for a value that could be a string or a number).

Design

  • Right-biased: map and flatMap operate on the Right value, leaving Left unchanged
  • Extends Container like all other types in this library
  • Provides mapLeft for transforming the Left side
  • Pattern matching via match(onLeft, onRight)
  • Safe extraction via getOrElse(fallback)

API

  • Either.Left(value) / Either.Right(value) — constructors
  • isLeft() / isRight() — type checks
  • map(fn) — transform Right
  • mapLeft(fn) — transform Left
  • flatMap(fn) — chain on Right
  • match(onLeft, onRight) — pattern match
  • getOrElse(fallback) — extract or default
  • toString()Left(<value>) or Right(<value>)

Implement Either<Left, Right> as a disjunction type that represents
a value that is one of two possible types, without implying error
semantics (unlike Result which implies success/failure).

Either is Right-biased: map and flatMap operate on the Right value.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant