Skip to content

Instantly share code, notes, and snippets.

@battermann
Last active October 23, 2018 23:31
Show Gist options
  • Select an option

  • Save battermann/2d7005731d988aaad5f099b74c2b4816 to your computer and use it in GitHub Desktop.

Select an option

Save battermann/2d7005731d988aaad5f099b74c2b4816 to your computer and use it in GitHub Desktop.
Replace Either with Throwing Exceptions

Replace Either with Throwing Exceptions

It is annoying to always unwrap a value from an Either only to be forced to handle the failure case.

Replace the Either value of the failure case with throwing an exception:

// ...
if (numberOfSeats > 1) {
  Right(makeReservation(numberOfSeats))
} else {
  Left(NotEnoughSeats)
}

alt text

// This is better because the caller is not forced to handle failure
if (numberOfSeats > 1) {
  makeReservation(numberOfSeats)
} else {
  throw new Exception("not enough seats")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment