From d7fd9375f29facdfa584cf4571b917dfaaf68c87 Mon Sep 17 00:00:00 2001 From: "junie-eap[bot]" Date: Fri, 6 Jun 2025 12:18:02 +0000 Subject: [PATCH] feat: implement getOrElseThrow in Either class The `getOrElseThrow` method was successfully implemented in the Either class, which returns the right value or throws an exception for the left value. The implementation was validated by passing relevant tests, and the project guidelines were followed through proper build processes. All changes have now been submitted successfully. --- .../commonMain/kotlin/arrow/core/Either.kt | 26 +++++++++++++++++++ .../kotlin/arrow/core/EitherTest.kt | 15 +++++++++++ 2 files changed, 41 insertions(+) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt index 7d6f69ad694..ac775b1586d 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt @@ -1344,6 +1344,32 @@ public inline infix fun Either.getOrElse(default: (A) -> B): B { } } +/** + * Returns the right value [B] of this [Either], + * or throws an exception produced by applying the [onLeft] function to the left value [A]. + * + * ```kotlin + * import arrow.core.Either + * import arrow.core.getOrElseThrow + * import io.kotest.assertions.throwables.shouldThrow + * import io.kotest.matchers.shouldBe + * + * fun test() { + * Either.Right(12).getOrElseThrow { RuntimeException("Error: $it") } shouldBe 12 + * shouldThrow { + * Either.Left("error").getOrElseThrow { RuntimeException("Error: $it") } + * }.message shouldBe "Error: error" + * } + * ``` + */ +public inline fun Either.getOrElseThrow(onLeft: (A) -> Throwable): B { + contract { callsInPlace(onLeft, InvocationKind.AT_MOST_ONCE) } + return when (this) { + is Left -> throw onLeft(this.value) + is Right -> this.value + } +} + /** * Returns the value from this [Right] or [Left]. * diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt index 8e570bcec97..fa6fae53ff1 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt @@ -129,6 +129,21 @@ class EitherTest { Left(a).getOrElse { b } shouldBe b } } + + @Test + fun getOrElseThrowOk() = runTest { + checkAll(Arb.int()) { a: Int -> + Right(a).getOrElseThrow { RuntimeException("Error: $it") } shouldBe a + + val left: Either = Left("error") + try { + left.getOrElseThrow { RuntimeException("Error: $it") } + fail("Should have thrown an exception") + } catch (e: RuntimeException) { + e.message shouldBe "Error: error" + } + } + } @Test fun getOrNullOk() = runTest {