Stylianos Gakis
05/31/2024, 4:55 PMEither<NonEmptyList<SomeError>, ValidResponse>
I have now done something like this
return either {
val errors = buildList {
if (hasError1) { add(SomeError.One) }
if (hasError2) { add(SomeError.Two) }
}.toNonEmptyListOrNull()
if (errors != null) { raise(errors) }
ValidResponse(...)
}
Is there some better way rather than doing this error list building dance? Or does this look good enough?Emil Kantis
05/31/2024, 5:34 PMzipOrAccumulate
like this:
zipOrAccumulate(
{ if (hasError1) raise("foo") },
{ if (hasError2) raise("bar") },
) { _, _ ->
"No error.."
}
Emil Kantis
05/31/2024, 5:38 PMfun <E, T> Raise<Nel<E>>.raiseAccumulate(block: RaiseAccumulate<E>.() -> T): T = RaiseAccumulate(this).block()
you might be able to do just
raiseAccumulate {
if (hasError1) raise("foo")
if (hasError2) raise("bar")
}
println("No errors!")
--- edit: that didn't work..Alejandro Serrano.Mena
05/31/2024, 5:49 PMStylianos Gakis
05/31/2024, 8:13 PMEither.zipOrAccumulate
but that was not what I was looking for after all.
Turns out there is in fact one with the signature Raise<NonEmptyList<Error>>.zipOrAccumulate
so this is exactly what I was looking for indeed, thanks a lot!
How is this going to be easier or 2.0? Really curious to learn more.Stylianos Gakis
06/02/2024, 9:56 AMAlejandro Serrano.Mena
06/02/2024, 3:24 PMStylianos Gakis
06/02/2024, 7:26 PM