Python Result: create aliases to simplify migration from kotlin Result

GitOrigin-RevId: 64b9ad7f1d458253c7d43bef74bef9f8fd88f697
This commit is contained in:
Ilya.Kazakevich
2024-11-13 19:24:52 +00:00
committed by intellij-monorepo-bot
parent a8afc1e335
commit e271220bac
@@ -87,14 +87,21 @@ sealed class Result<out SUCC, out ERR> {
/**
* Like Rust `unwrap`: returns result or throws exception. Use when error is unexpected
*/
fun orThrow(onError: (ERR) -> Throwable = { e -> if (e is Throwable) e else AssertionError(e) }): SUCC {
fun orThrow(onError: (ERR) -> Throwable = { e -> if (e is Throwable) e else AssertionError(e) }): SUCC {
when (this) {
is Success -> return result
is Failure -> throw onError(this.error)
}
}
// To be backward compatible with Kotlin result
companion object {
fun <S> success(value: S) = Success(value)
fun <E> failure(error: E) = Failure(error)
}
}
/**
* Maps success result to another one with same error
* ```kotlin