Is there any appetite for something similar to thi...
# arrow
m
Is there any appetite for something similar to this in the library?
Copy code
inline 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.
y
That's just
recover
that ignores its argument though! It can be written simpler as such:
Copy code
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
)
m
Here's a snippet of how I'd use it. Thanks for the suggestions BTW. I appreciate them.
Copy code
override 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)
  }
y
So I think you want something like:
Copy code
inline fun <Error, T> flippedRecover(
  recover: (Error) -> T,
  block: Raise<Error>.() -> T
) = recover(block, recover)
and then you use it like:
Copy code
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.
m
> The
_: 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.
y
arrow intensifies 1
thank you color 1