Are we missing a `catch(block, transform, catch)` ...
# arrow-contributors
p
Are we missing a
catch(block, transform, catch)
override? Currently doing the equivalent of
catch({ transform(block()) }, catch)
would result in any exceptions from
transform
being handled by the
catch
function.
a
I think that one is simply
fold
p
I thought so too, but
fold
is more as it introduces a redundant
Raise<Error>
and supporting mechanism
I might push a draft PR with
Copy code
@RaiseDSL
public inline fun <A, B> catch(block: () -> A, transform: (value: A) -> B, catch: (throwable: Throwable) -> B): B {
  contract {
    callsInPlace(block, AT_MOST_ONCE)
    callsInPlace(transform, AT_MOST_ONCE)
    callsInPlace(catch, AT_MOST_ONCE)
  }
  val value = catch({ block() }, { return catch(it) })
  return transform(value)
}
to discuss
a
I just want to be sure we're just not adding overloads just for the sake of it
1
p
Fully with you - this only came up as I had a case that needed it (in my case the "transform" shouldn't throw anything but it still felt wrong to have it within the block, but I still wanted to the catch to return the appropriate type)
I'd have happily used
fold
but I had to provide
Nothing
for
Error
and realised it would create a redundant
DefaultRaise
etc
s
I just want to be sure we're just not adding overloads just for the sake of it
I'm open to reviewing the base functions, and seeing if we need to rename or introduce some new signatures for 2.3. I would also consider deprecating any confusing ones because since releasing them we've seen some confusion and gotten repeated questions about recover, catch & fold and when to use what.
p
I did push a PR with this overload in https://github.com/arrow-kt/arrow/pull/3751 as in my mind it fills the gap that's covered for the
recover
and
fold
but not for
catch
- if there's to be a review for 2.3 I think it would be good to include it either in 2.2 or at least ensure the use-case is covered by any review of signatures.