From e271220bacee3efbd4ddaa5be6fd23439c71d69b Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Wed, 13 Nov 2024 17:51:26 +0100 Subject: [PATCH] Python Result: create aliases to simplify migration from kotlin Result GitOrigin-RevId: 64b9ad7f1d458253c7d43bef74bef9f8fd88f697 --- python/openapi/src/com/jetbrains/python/Result.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/python/openapi/src/com/jetbrains/python/Result.kt b/python/openapi/src/com/jetbrains/python/Result.kt index 6c7d526474c3..c6c2fa6dcb1c 100644 --- a/python/openapi/src/com/jetbrains/python/Result.kt +++ b/python/openapi/src/com/jetbrains/python/Result.kt @@ -87,14 +87,21 @@ sealed class Result { /** * 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 success(value: S) = Success(value) + fun failure(error: E) = Failure(error) + } } + /** * Maps success result to another one with same error * ```kotlin