kittinunf / Result

The modelling for success/failure of operations in Kotlin and KMM (Kotlin Multiplatform Mobile)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Map that throws?

macduy opened this issue · comments

Hi, I was surprised to find that this code would not print "Error":

val result = Result<Int, Exception>.of(0)
result.map {
    transform(it)   // transform throws
}.failure {
    print("Error")
}

fun transform(i: Int) {
    throw Exception()
}

It looks like map does not expect to handle errors. What I had to do instead was:

val result = Result<Int, Exception>.of(0)
result.flatMap {
    Result.of {
        transform(it)
    }
}.failure {
    print("Error")
}

which looks less elegant. More importantly, in the first case, there is no error shown anywhere so it's difficult to debug.

Hmm, I think this is an overlook from me. I very much agree with you. I will try to address this in the next version.

Thanks Kittinun!

This should be addressed by #30. Thanks for reporting this! :)

Ah that's awesome. Thank you, great work!