Suppose I have two functions that both return an E...
# arrow
k
Suppose I have two functions that both return an Either<Error, Something> and I want to call both functions anf then accumulate the errors before executing further code if both Either’s are
Right
. What’s the most elegant way to do this, I think I know how to short circuit the computation but in this case I have to be able to gather all the errors
k
Thanks, I’ve looked at the typed-errors docs previously but I can’t figure out how to get that to work in this scenario
a
zipOrAccumulate should work there
k
I have looked at zipOrAccumulate but I don’t think I that works for me here. I need to go away and have another think about this. I think what I’m trying to do doesn’t fit exactly into the way Arrow works with error handling and I may need to handle it slightly differently
s
Your original question makes it sound like zipOeAccumulate should work. It'd be interesting to explain how it doesn't work for you to understand what works differently in your case.
k
Having thought about this further, and understanding zipOrAccumulate a bit better I’ve come up with this. It seems ugly-ish but it gives me what I want. The !! on the leftOrNull isn’t pretty, and neither is returning a
Pair
(there are times I’ll want to validate more than two objects) so, is this going in the right direction and if so how do I improve the code?
Copy code
// matchType is Either<MatchTypeError, MatchType>
// countryId is Either<CountryIdError, CountryId>
either {
    zipOrAccumulate(
        {
            ensure(matchType.isRight()) { matchType.leftOrNull()!! }
            matchType.bind()
        }, {
            val a: Int = 0
            ensure(countryId.isRight()) { countryId.leftOrNull()!! }
            countryId.bind()
        }
    ) { matchType, countryId ->
        Pair(matchType, countryId)
    }
}
d
Bind is supposed to do that ensure for you...
If matchType is right, bind returns it, otherwise it raises left
And that would get accumulated in the zipOrAccumulate...
k
Ah, thank you, that’s so much better
👍🏼 1