You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
functioncastRight_of_Left<RT>(either: Either<Error,unknown>){returnLeft<Error,RT>(either.left())}publicasyncgetOrder(orderNumber: string): Promise<Either<Error,Order>>{let result =awaitthis._orderRepo.getOrderByOrderNumber(orderNumber)// returns Promise<Either<Error, Order | null>>if(result.isLeft())returncastRight_of_Left<Order_and_Sample>(result)// Database error, pass the error up the call chainif(getSampleresult.right()==null)returnLeft(newError(`order not found`))// aanvraag niet gevondenconstorder=result.right()!// ... manipulations of the order objectreturnRight(order)}
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: