https://kotlinlang.org logo
#stdlib
Title
# stdlib
a

Andy McGhie

07/04/2022, 11:31 PM
As per the suggestion cross-posting into #stdlib
e

ephemient

07/04/2022, 11:35 PM
Copy code
result.recoverCatching { exception ->
    throw SomeOtherException(exception)
}
a

Andy McGhie

07/04/2022, 11:36 PM
Isn't that slightly inefficient from a processing perspective as the JVM will need to generate the stacktrace etc?
e

ephemient

07/04/2022, 11:49 PM
the stacktrace is gathered when
SomeOtherException
is constructed, so you'll have that cost even without the
throw
.
(but yes, exceptional control flow is more expensive, just not for that reason)
if that matters, you can still use
Copy code
result.fold(onSuccess = { Result.success(it) }, onFailure = { Result.failure(transform(it)) })
instead of your original
a

Andy McGhie

07/04/2022, 11:58 PM
Thanks @ephemient we've been using the
fold
method in our code, was just hoping for a slightly cleaner/more elegant solution