Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[request for help] - casting right type on left #251

Open
stefaanMLB opened this issue Jan 16, 2022 · 1 comment
Open

[request for help] - casting right type on left #251

stefaanMLB opened this issue Jan 16, 2022 · 1 comment

Comments

@stefaanMLB
Copy link

I'm new to functional programming and I am probably doing this all wrong.
I very much like using Either to pass errors up the call chain.
In practice, I find myself using a type cast of the right side when the either is a Left very often which feels wrong.
This typically happens when a function in the middel of the call chain catches a left from a function down the chain that has a different Right type than the function itself.
I assume there must be a better way of doing this otherwise the cast function would probably be part of the library.

What is the better way ?

Here's an simplified example

function castRight_of_Left<RT>(either: Either<Error, unknown>) {
  return Left<Error, RT>(either.left())
}

public async getOrder(orderNumber: string): Promise<Either<Error, Order>> {
  let result = await this._orderRepo.getOrderByOrderNumber(orderNumber)             // returns Promise<Either<Error, Order | null>>
  if (result.isLeft()) return castRight_of_Left<Order_and_Sample>(result)           // Database error, pass the error up the call chain
  if (getSampleresult.right() == null) return Left(new Error(`order not found`))    // aanvraag niet gevonden
  const order = result.right()!

  // ... manipulations of the order object

  return Right(order)
}
@stefaanMLB
Copy link
Author

Anyone ?

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

No branches or pull requests

1 participant