Rob Elliot03/02/2022, 3:32 PM
instances into a single Result? I can write it myself but a shame to repeat it. I want roughly the following:
So the function would look something like:
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)
But obviously it would need loads of overloads.
fun <A, B, T> reduce(a: Result<A>, b: Result<B>, work: (A, B) -> T): Result<T>
Joffrey03/02/2022, 3:35 PM
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 Elliot03/02/2022, 3:36 PM
style of error reporting do in this case?
Joffrey03/02/2022, 3:40 PM
(after a paragraph mentioning the Arrow library and kittinunf's
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.
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 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 a
Resulttype 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.
, but I would tend to think so.
Rob Elliot03/02/2022, 3:45 PM
Matthew Gast03/02/2022, 3:47 PM
Rob Elliot03/02/2022, 3:49 PM
looks like exactly what I want.
Alex Prince03/02/2022, 4:12 PM