Rob Elliot
03/02/2022, 3:32 PMResult
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.Joffrey
03/02/2022, 3:35 PMResult
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.Rob Elliot
03/02/2022, 3:36 PMEither
style of error reporting do in this case?Joffrey
03/02/2022, 3:40 PMResult
(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 returningSo I guess people who like the railway style are encouraged to use libraries like Arrow,Try
, 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.Result
Joffrey
03/02/2022, 3:42 PMUPDATE: 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 theIt's unclear whether we can conclude that it's unlikely we'll get extensive helper functions fortype. As a part of the standard library aResult
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.Result
Result
, but I would tend to think so.Rob Elliot
03/02/2022, 3:45 PMMatthew Gast
03/02/2022, 3:47 PMRob Elliot
03/02/2022, 3:49 PMbinding
looks like exactly what I want.Alex Prince
03/02/2022, 4:12 PM