https://kotlinlang.org logo
Title
r

Rob Elliot

03/02/2022, 3:32 PM
Is there a way in the stdlib to reduce a bunch of
Result
instances into a single Result? I can write it myself but a shame to repeat it. I want roughly the following:
data class Foo(val a: String, val b: Int)

val resultA: Result<String> = TODO()
val resultB: Result<Int> = TODO()

val foo: Result<Foo> = reduce(resultA, resultB, ::Foo)
So the function would look something like:
fun <A, B, T> reduce(a: Result<A>, b: Result<B>, work: (A, B) -> T): Result<T>
But obviously it would need loads of overloads.
j

Joffrey

03/02/2022, 3:35 PM
I don't think there is any facilities for this. The built-in
Result
class is not meant to be used in business code, it's mostly targeted to framework code that need to report/transfer errors in a special way (such as the coroutine library). That's why I believe there is no real incentive to add convenience facilities on top of this class in the stdlib.
r

Rob Elliot

03/02/2022, 3:36 PM
OK - what do people who like the
Either
style of error reporting do in this case?
j

Joffrey

03/02/2022, 3:40 PM
From the design document of
Result
(after a paragraph mentioning the Arrow library and kittinunf's
Result<T,E>
):
Note, that both of the libraries above promote "Railway Oriented Programming" style with monads and their transformations, which heavily relies on functions returning
Try
,
Result
, etc. This programming style can be implemented and used in Kotlin via libraries as the above examples demonstrate. However, core Kotlin language and its Standard Library are designed around a direct programming style in mind. The general approach in Kotlin is that alternative programming styles should be provided as 3rd party libraries and DSLs.
So I guess people who like the railway style are encouraged to use libraries like Arrow
Also in the same doc, on the topic of special built-in language syntax for `Result`:
UPDATE: We have reached a decision of not following this road for a foreseeable future and will not pursue integration of any special constructions into the language that are tied to the
Result
type. As a part of the standard library a
Result
type will stay narrowly-focused on the use-cases that are described at the beginning of this document, abandoning ambitions to become any kind of universal error-handling primitive. We are working in other directions of making signalling error handling more pleasant to use in Kotlin.
It's unclear whether we can conclude that it's unlikely we'll get extensive helper functions for
Result
, but I would tend to think so.
r

Rob Elliot

03/02/2022, 3:45 PM
Cool, thanks. I'll look at Arrow's stuff. Seems https://arrow-kt.io/docs/patterns/error_handling/ has some useful thoughts.
👍 1
m

Matthew Gast

03/02/2022, 3:47 PM
If you're looking for a direct replacement of Result (without all of the extra Arrow functionality), check out kotlin-result: https://github.com/michaelbull/kotlin-result
👍 3
r

Rob Elliot

03/02/2022, 3:49 PM
Cool,
binding
looks like exactly what I want.
a

Alex Prince

03/02/2022, 4:12 PM
yep map/bind depending which return signature you need