recover
infix fun <E, E2, A> Effect<E, A>.recover(resolve: suspend Raise<E2>.(raised: E) -> A): Effect<E2, A>
Catch the raised value E of the Effect. You can either return a value a new value of A, or short-circuit the effect by raising with a value of E, or raise an exception into suspend.
import arrow.core.raise.effect
import arrow.core.raise.recover
object User
object Error
val error = effect<Error, User> { raise(Error) } // Raise(error)
val a = error.recover<Error, Error, User> { error -> User } // Success(User)
val b = error.recover<Error, String, User> { error -> raise("other-failure") } // Raise(other-failure)
val c = error.recover<Error, Nothing, User> { error -> throw RuntimeException("BOOM") } // Exception(BOOM)Content copied to clipboard
infix fun <E, E2, A> EagerEffect<E, A>.recover(resolve: Raise<E2>.(raised: E) -> A): EagerEffect<E2, A>
inline fun <R, E, A> Raise<R>.recover(action: Raise<E>.() -> A, recover: Raise<R>.(E) -> A, catch: Raise<R>.(Throwable) -> A): A
Execute the Raise context function resulting in A or any logical error of type E, and recover by providing a fallback value of type A or raising a new error of type R.
suspend fun test() {
either<Nothing, Int> {
recover({ raise("failed") }) { str -> str.length }
} shouldBe Either.Right(6)
either {
recover({ raise("failed") }) { str -> raise(-1) }
} shouldBe Either.Left(-1)
}Content copied to clipboard