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

`flatMapError` should take value type (V) to consideration

alexzaitsev opened this issue · comments

Lib version com.github.kittinunf.result:result-jvm:5.1.0

flatMapError returns * type for value that makes it impossible to use in chains. Original implementation was useless for me, I had to provide my own:

inline fun <V, reified E : Throwable, reified EE : Throwable> Result<V, E>.rightFlatMapError(
    transform: (E) -> Result<V, EE>
): Result<V, EE> =
    try {
        when (this) {
            is Result.Success -> Result.success(value)
            is Result.Failure -> transform(error)
        }
    } catch (ex: Exception) {
        when (ex) {
            is EE -> Result.failure(ex)
            else -> throw ex
        }
    }

Just compare to original:

inline fun <reified E : Throwable, reified EE : Throwable> Result<*, E>.flatMapError(transform: (E) -> Result<*, EE>): Result<*, EE> = try {
    when (this) {
        is Result.Success -> Result.success(value)
        is Result.Failure -> transform(error)
    }
} catch (ex: Exception) {
    when (ex) {
        is EE -> Result.failure(ex)
        else -> throw ex
    }
}

Btw, thanks for the lib! Fix it, please.

Yes sorry about that! it has been fixed in #92. Would you mind wait for the new artifact this week 23 Aug? 🙏

Close with new version