I don't see a channel dedicated to the kotlin-resu...
# arrow
d
I don't see a channel dedicated to the kotlin-result library so allow me to ask here because kotlin-result is partly inspired by Arrow's Either so the problem might be similar in both frameworks. In my controller, when I get a Result, I want to either throw an error in case of Err, or transform Ok. Is there a nicer way than this?
Copy code
myResult
  .getOrThrow { createException() }
  .let { transform(it) }
I was thinking of something similar to
mapBoth
which, however, maps both Ok and Err to a common type.
y
you're likely looking for a
fold
function. I'm not sure if
kotlin-result
provides it, but the implementation is likely trivial.
d
fold
is the same as
mapBoth
and as I said I do want to throw an exception for the error case, not a result.
y
Fold isn't the same though. It deconstructs the whole value, and so
fold(transform) { throw createException() }
will do the trick
d
Well, in kotlin-result they are the same 🙂 https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt#L127 Anyway, it turns out that I can indeed use this for my use case. I just must have made some silly mistake in trying so yesterday...