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.
It's common when validating multiple inputs (something like form validation) to have many possible
results
where you can only continue if they're allok
and you need to effectively short-circuit on anyerr
.Using
Result::mapOk
andResult::flatMapOk
can be handy, but when you need to refer to the unwrappedok
values all within one scope, you end up with the "pyramid of doom" nested indentation:An alternative approach is to check each intermediate
result
and return early with guard clauses which is much more readable but we end up having a pile of extra code blocks,isErr
checks, and then having tounwrapOrElseThrow()
a bunch of times at the end:This PR adds a
Result::combine
utility method that let's you combine up to 9 separateresults
of any type, operate on them all within a single mapping function, and return a newresult
, short-circuiting if any of the inputresults
contained errors.