https://kotlinlang.org logo
#arrow
Title
# arrow
d

dave08

02/21/2023, 4:46 PM
To catch an exception the right thing is to use
client.runCatching { get("/") }.bind()
?
p

Petr Laštovička

02/21/2023, 4:47 PM
I would suggest Either.catch
d

dave08

02/21/2023, 4:49 PM
But it returns an
A
not `R`:
Copy code
public inline fun <R, A> Raise<R>.catch(
  @BuilderInference action: Raise<R>.() -> A,
  @BuilderInference catch: Raise<R>.(Throwable) -> A,
)
p

Petr Laštovička

02/21/2023, 4:50 PM
Oh, I see, I have forgotten the exact syntax
c

CLOVIS

02/21/2023, 4:51 PM
Never use kotlin.runCatching with coroutines. They are not compatible.
runCatching
catches
CancellationException
, which is forbidden by the coroutines library (it breaks structured concurrency). To my knowledge Arrow's
Either.catch
is fine, but the arrow maintainers can confirm
d

dave08

02/21/2023, 4:56 PM
Oh... I just got it:
Copy code
val response = catch({ request() }, { raise(ServerIsDown) })
I had to use
raise(..)
, it seems like catch is also a recover mechanism if you return a valid right value instead of raising.
p

phldavies

02/21/2023, 5:05 PM
Indeed,
Either.catch
uses
t.nonFatalOrThrow().left()
to capture the thrown
Throwable
, and rethrow it if it's considered "fatal" (including cancellation) - based on the platform-specific implementation of a NonFatal predicate.
s

simon.vergauwen

02/21/2023, 5:28 PM
it seems like catch is also a recover mechanism if you return a valid right value instead of raising.
Yes, all these APIs are the same. Where
catch
works over
Throwable
, and
recover
over
E
.
catch
also exists over a
reified E
in case you only want to
catch
specific errors.
It would be nice to have a KDOC on
catch()
explaining that... it's not really clear from it's name.
Which documentation are you looking at? It's explained in the
catch
function on
Either<Throwable, A>
is it missing from the DSL version?
d

dave08

02/22/2023, 10:15 AM
Both don't seem to be documented... when discovering a new library one of the first things I do is try
this.
and see what Idea pulls up, then I press ctrl-q to get the kdocs in a popup window... it's MUCH easier and faster to discover a new api than to go back and forth to the docs site...
If they're both the same, will one be deprecated?
If they're both the same, will one be deprecated?
One is DSL version, and one is top-level so both will exists alongside each-other.
d

dave08

02/22/2023, 10:18 AM
image.png
image.png
First is in Either, second is in Raise... maybe it was documented after alpha 28?
Or these are overloads?
One is DSL version, and one is top-level so both will exists alongside each-other. (edited)
I see them both in the DSL
s

simon.vergauwen

02/22/2023, 10:21 AM
These are all overloads, but I see only
recover
is documented and not
catch
.
Adding
/** @see catch */
into the KDoc doesn't help since it's ambiguous for overloads and duplicating documentation doesn't seem like a good solution either.
Either.Companion.catch
is indeed undocumented.
d

dave08

02/22/2023, 10:22 AM
Maybe some build hook to add the docs before publishing to Maven..? (like maybe if it would process those
/** @see ...*/
s...)
I wonder if there are others that have this same problem...
since it's ambiguous for overloads
Adding a
@JvmName
doesn't help there? 🤞🏼
c

CLOVIS

02/22/2023, 10:26 AM
No, KDoc links use the Kotlin name
It's on purpose, so overloads appear in a single page in the documentation (this way you can easily see what overloads of the function you're looking at exist, which is very hard in the old Javadoc format)
Since overloads are supposed to do the same thing, they're expected to appear together and it shouldn't matter which one the user is looking at I think you can disambiguate by explicitly adding the receiver if it's different though 🤔
d

dave08

02/22/2023, 10:29 AM
But Intellij isn't as smart to know how to pull those all together...
c

CLOVIS

02/22/2023, 10:29 AM
It should autocomplete all of them? But indeed, it won't magically add the documentation to ones that aren't documented.
I tend to duplicate a short description of what the function does, then link to the containing class or object where I put the detailed explanation
13 Views