From fca2d618dcdf37476f90a00edc53e8406ea40fa5 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Mon, 22 Aug 2022 12:38:50 +0200 Subject: [PATCH] Stop relying on getOrNull as generic failure signal --- .../commonMain/kotlin/arrow/core/Result.kt | 55 ++++++++++--------- .../kotlin/arrow/core/ResultTest.kt | 15 +++++ 2 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/ResultTest.kt diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Result.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Result.kt index 6947a89675f..c787c60f447 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Result.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Result.kt @@ -155,6 +155,7 @@ public inline fun Result.zip( ): Result = zip(b, c, d, e, f, g, h, i, UnitResult) { a, b, c, d, e, f, g, h, i, _ -> transform(a, b, c, d, e, f, g, h, i) } +@Suppress("UNCHECKED_CAST") public inline fun Result.zip( b: Result, c: Result, @@ -165,31 +166,35 @@ public inline fun Result.zip( h: Result, i: Result, j: Result, - transform: (A, B, C, D, E, F, G, H, I, J) -> K -): Result = Nullable.zip( - getOrNull(), - b.getOrNull(), - c.getOrNull(), - d.getOrNull(), - e.getOrNull(), - f.getOrNull(), - g.getOrNull(), - h.getOrNull(), - i.getOrNull(), - j.getOrNull(), - transform -)?.let { success(it) } ?: composeErrors( - exceptionOrNull(), - b.exceptionOrNull(), - c.exceptionOrNull(), - d.exceptionOrNull(), - e.exceptionOrNull(), - f.exceptionOrNull(), - g.exceptionOrNull(), - h.exceptionOrNull(), - i.exceptionOrNull(), - j.exceptionOrNull(), -)!!.let(::failure) + transform: (A, B, C, D, E, F, G, H, I, J) -> K, +): Result = + if (isSuccess && b.isSuccess && c.isSuccess && d.isSuccess && e.isSuccess && f.isSuccess && g.isSuccess && h.isSuccess && i.isSuccess && j.isSuccess) + success( + transform( + getOrNull() as A, + b.getOrNull() as B, + c.getOrNull() as C, + d.getOrNull() as D, + e.getOrNull() as E, + f.getOrNull() as F, + g.getOrNull() as G, + h.getOrNull() as H, + i.getOrNull() as I, + j.getOrNull() as J + ) + ) else + composeErrors( + exceptionOrNull(), + b.exceptionOrNull(), + c.exceptionOrNull(), + d.exceptionOrNull(), + e.exceptionOrNull(), + f.exceptionOrNull(), + g.exceptionOrNull(), + h.exceptionOrNull(), + i.exceptionOrNull(), + j.exceptionOrNull(), + )!!.let(::failure) @PublishedApi internal fun composeErrors(vararg other: Throwable?): Throwable? = diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/ResultTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/ResultTest.kt new file mode 100644 index 00000000000..af57748b263 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/ResultTest.kt @@ -0,0 +1,15 @@ +package arrow.core + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class ResultTest : StringSpec({ + + "null zip null" { + val x = Result.success(null) + x.zip(x) { y, z -> + (y?.plus(z ?: -2)) ?: -1 + } shouldBe Result.success(-1) + } + +})