Add Result utils

pull/223/head
Koitharu 3 years ago
parent d24ab347cb
commit 03b4fc9f00
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -13,3 +13,17 @@ inline fun <T, R> T.runCatchingCancellable(block: T.() -> R): Result<R> {
Result.failure(e)
}
}
inline fun <R, T : R> Result<T>.recoverCatchingCancellable(transform: (exception: Throwable) -> R): Result<R> {
return when (val exception = exceptionOrNull()) {
null -> this
else -> runCatchingCancellable { transform(exception) }
}
}
inline fun <R : Any, T : R> Result<T>.recoverNotNull(transform: (exception: Throwable) -> R?): Result<R> {
return when (val exception = exceptionOrNull()) {
null -> this
else -> transform(exception)?.let(Result.Companion::success) ?: this
}
}

Loading…
Cancel
Save