Manuel
09/19/2025, 12:01 AMinline fun <T> recover(
recover: () -> T,
block: IgnoreErrorsRaise<Null>.() -> T,
): T = nullable { ignoreErrors { block() } } ?: recover()
Maybe it already exists? I'm still on Arrow v1.2.4. It's a pattern I find myself using often but the nullable reads a bit weird when you see it inlined nullable { } ?: fallback because the DSL isn't being used for any kind of nullability, just convenience.Youssef Shoaib [MOD]
09/19/2025, 12:07 AMrecover that ignores its argument though! It can be written simpler as such:
inline fun <T> recover(
recover: () -> T,
block: Raise<Any?>.() -> T,
): T = recover(block) { recover() }
I think what you want is probably a flipped recover for nicer syntax? I've been thinking about that for a while, but #C0474L1Q8DR is hard.
(Btw, yours acts weird if your block returns null, but maybe that's what you want. Mine only calls recover if something is raised)
(Mine also doesn't include the IgnoreErrorsRaise part. In Arrow 2.X we have SingletonRaise serving the same purpose. The recover-like thing for SingletonRaise is singleton)Manuel
09/19/2025, 12:12 AMoverride fun intercept(chain: Interceptor.Chain): Response = runBlocking {
val request = chain.request()
val modifiedRequest =
recover(recover = { request }) {
ensure(observeUserRegistrationState().first() is UserRegistered)
val token =
authenticationRepository.getToken()
.onLeft { Timber.e("AuthHeadersInterceptor: $it") }
.bind()
request
.apply {
header(
"Authorization",
"Bearer ${token.value}",
)
}
.build()
}
chain.proceed(modifiedRequest)
}Youssef Shoaib [MOD]
09/19/2025, 12:14 AMinline fun <Error, T> flippedRecover(
recover: (Error) -> T,
block: Raise<Error>.() -> T
) = recover(block, recover)
and then you use it like:
flippedRecover({ _: Any? -> request }) {
...
}
and inside (once the newest Arrow version with contexts land) you'd be able to use the singleton versions of ensure and be able to bind `null`s and such, and still be able to bind any `Left`s you want.
The _: Any? part is mildly annoying though, so I see that maybe you want a recoverAllErrors function.Manuel
09/19/2025, 12:16 AM_: Any? part is mildly annoying though, so I see that maybe you want a recoverAllErrors function.
Yes! I'd like to avoid the clutter of unnecessary raises or empty trailing lambdas as well.Youssef Shoaib [MOD]
09/19/2025, 12:37 AM