If I got a function that returns a `Either<NonE...
# arrow
s
If I got a function that returns a
Either<NonEmptyList<SomeError>, ValidResponse>
I have now done something like this
Copy code
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?
e
I think you can use
zipOrAccumulate
like this:
Copy code
zipOrAccumulate(
         { if (hasError1) raise("foo") },
         { if (hasError2) raise("bar") },
      ) { _, _ ->
         "No error.."
      }
I'm wondering if this is perhaps an option too.. not sure if it exists in the library but if you add this function
Copy code
fun <E, T> Raise<Nel<E>>.raiseAccumulate(block: RaiseAccumulate<E>.() -> T): T = RaiseAccumulate(this).block()
you might be able to do just
Copy code
raiseAccumulate {
         if (hasError1) raise("foo")
         if (hasError2) raise("bar")
      }

      println("No errors!")
--- edit: that didn't work..
a
zipOrAccumulate is the answer here... we're working on making this even easier in Arrow 2.0
2
👍 4
s
I was trying to find something like that, but I was getting the
Either.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.
Is this "accumulating" dsl showcased here https://x.com/vergauwen_simon/status/1797197274369466417?t=HXOElL7Jx6eaVOOP2O3XWg&s=1 what will make this easier in arrow 2? This looks fantastic btw!
🤩 3
a
yes, and if you want to have a peek at it, here's the PR https://github.com/arrow-kt/arrow/pull/3436
s
What can I say, it's looking amazing!
🥰 1