Propose Try.toEither(Throwable => L) #2724
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thank You
Thank you for all of the work on Vavr! It has quickly become my favorite Java library, and usually the first thing I add to every new project ❤️ Aside from the benefit to my own code, it has also been a great tool to help teach others about FP and get them curious about it.
Proposal / What is this?
I come across cases where I want to convert a
Try
to anEither
while mapping theFailure
value onto aLeft
value.One example would be safely handling code which isn't mine, then mapping the
Throwable
onto an error type.I do this normally either with
fold(throwableMapper, identity())
or.toEither().mapLeft(throwableMapper)
, but find that.toEither(throwableMapper)
would be a nice shortcut to replace either case.I have also a few colleagues who would appreciate the addition from a readability standpoint I think, so I thought I'd suggest it.
Implementation
By my own style, I'd normally just write this as a wrapper around a
fold
call. However, as a first contribution, I am trying to stick as close as possible to the style I already see in the code here.By that reasoning, the code style, namings, and Javadoc are all created based on examples I find existing within the file.
Since the
toEither(Supplier)
implementation assumes theSupplier
is pure/will not throw an exception and does not handle such a case, I have followed the same assumption here as well.