Hi fellow Arrow users! I wonder is there any examp...
# arrow
o
Hi fellow Arrow users! I wonder is there any example or intention of using Try monad from Scala in Arrow? It would be really good addition. So far, I am using a custom implementation and conversions between arrow and Try: https://github.com/Trendyol/stove4k/blob/main/lib/stove-testing-e2e/src/main/kotlin/com/trendyol/stove/functional/Try.kt
c
Hi!
Try
is essentially just an
Either
that catches exceptions. In Arrow, you can use:
Copy code
val foo: Either<Throwable, Int> = Either.catch {
    5 % 0
}
to catch exceptions with regular
Either
. Is there anything else from
Try
that you would find useful?
o
Yes you’re right. It pretty much does the same job. Do you think that it is a design decision about not implementing Try as a separate monad?
Either.catch
is a feature Scala does not implement as far as I know and offloads this burden to
Try
.
r
We used to have
Try
in Arrow in the 0.x series but we removed it a some point in favor of Kotlin's Result which models the same thing https://blog.rockthejvm.com/functional-error-handling-in-kotlin-part-2/
Copy code
Scala         Kotlin

Try(block) -> runCatching { ... }
Success(value) -> Result.success(value)
Failure(ex) -> Result.failure(e)
The main difference between Scala's Try and Result is that the
Try
constructor is implemented only capturing
NonFatal
exceptions: https://github.com/scala/scala/blob/13a80bd6faea2d9a53f40305853debd3900e729e/src/library/scala/util/Try.scala#L220 https://github.com/scala/scala/blob/13a80bd6faea2d9a53f40305853debd3900e729e/src/library/scala/util/control/NonFatal.scala#L42 In contrast the implementation of
runCatching
may capture exceptions that in most cases are considered Fatal and something you should. not recover from: https://github.com/JetBrains/kotlin/blob/e959e97992507f4ecc84eca3fab829692ec05376/libraries/stdlib/src/kotlin/util/Result.kt#L144 In Arrow for
Either.catch
and other variations of
catch
we check if the exception is fatal and if it is we rethrow it. https://github.com/arrow-kt/arrow/blob/731a9b292ab5f2f8ba2decc251dab3f2c4a60c06/ar[…]core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Raise.kt
Yes you’re right. It pretty much does the same job. Do you think that it is a design decision about not implementing Try as a separate monad?
Either.catch
is a feature Scala does not implement as far as I know and offloads this burden to
Try
.
The design decision here was motivated because
Try
was the same as
Result
but Result lacks a way to bubble up fatal exceptions with
nonFatalOrThrow
. We decided to remove
Try
, keep
nonFatalOrThrow
as public APIs in case you want to use them with
try/catch
and provide
catch
variations as functions and constructors for Error Handling related types.
gratitude thank you 1
o
Thanks a lot giving the decision history and detailed explanation!