https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
a

Andy McGhie

07/04/2022, 7:45 AM
When using a
Result
it's often useful to map an exception to another type of exception, has there any thought been given to adding something like
mapFailure
to the
Result
i.e.:
Copy code
inline fun <R> Result<R>.mapFailure(transform: (exception: Throwable) -> Throwable): Result<R> {
    return when {
        isFailure -> Result.failure(transform(exceptionOrNull()!!))
        else -> this
    }
}
Usage:
Copy code
Result
    .failure<String>(NullPointerException())
    .mapFailure {
        MyDomainException()
    }
m

Mikhail

07/04/2022, 3:17 PM
I think this should be in the #stdlib channel
2 Views