Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate Result zip method in favour of DSL #2977

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 100 additions & 3 deletions arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Result.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:OptIn(ExperimentalContracts::class)

package arrow.core

import kotlin.Result.Companion.failure
Expand Down Expand Up @@ -53,6 +54,13 @@ public inline fun <A, B> Result<A>.redeemWith(
/**
* Combines n-arity independent [Result] values with a [transform] function.
*/
@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result<A> {transform(this.bind(), b.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C> Result<A>.zip(b: Result<B>, transform: (A, B) -> C): Result<C> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(
Expand All @@ -68,6 +76,13 @@ public inline fun <A, B, C> Result<A>.zip(b: Result<B>, transform: (A, B) -> C):
) { a, b, _, _, _, _, _, _, _, _ -> transform(a, b) }
}

@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D> Result<A>.zip(b: Result<B>, c: Result<C>, transform: (A, B, C) -> D): Result<D> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(
Expand All @@ -83,6 +98,13 @@ public inline fun <A, B, C, D> Result<A>.zip(b: Result<B>, c: Result<C>, transfo
) { a, b, c, _, _, _, _, _, _, _ -> transform(a, b, c) }
}

@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind(), d.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D, E> Result<A>.zip(
b: Result<B>,
c: Result<C>,
Expand All @@ -103,6 +125,13 @@ public inline fun <A, B, C, D, E> Result<A>.zip(
) { a, b, c, d, _, _, _, _, _, _ -> transform(a, b, c, d) }
}

@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind(), d.bind(), e.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D, E, F> Result<A>.zip(
b: Result<B>,
c: Result<C>,
Expand All @@ -122,6 +151,13 @@ public inline fun <A, B, C, D, E, F> Result<A>.zip(
}
}

@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D, E, F, G> Result<A>.zip(
b: Result<B>,
c: Result<C>,
Expand All @@ -143,6 +179,13 @@ public inline fun <A, B, C, D, E, F, G> Result<A>.zip(
}
}

@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D, E, F, G, H> Result<A>.zip(
b: Result<B>,
c: Result<C>,
Expand All @@ -153,9 +196,26 @@ public inline fun <A, B, C, D, E, F, G, H> Result<A>.zip(
transform: (A, B, C, D, E, F, G) -> H
): Result<H> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, UnitResult, UnitResult, UnitResult) { a, b, c, d, e, f, g, _, _, _ -> transform(a, b, c, d, e, f, g) }
return zip(b, c, d, e, f, g, UnitResult, UnitResult, UnitResult) { a, b, c, d, e, f, g, _, _, _ ->
transform(
a,
b,
c,
d,
e,
f,
g
)
}
}

@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D, E, F, G, H, I> Result<A>.zip(
b: Result<B>,
c: Result<C>,
Expand All @@ -167,9 +227,27 @@ public inline fun <A, B, C, D, E, F, G, H, I> Result<A>.zip(
transform: (A, B, C, D, E, F, G, H) -> I
): Result<I> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, h, UnitResult, UnitResult) { a, b, c, d, e, f, g, h, _, _ -> transform(a, b, c, d, e, f, g, h) }
return zip(b, c, d, e, f, g, h, UnitResult, UnitResult) { a, b, c, d, e, f, g, h, _, _ ->
transform(
a,
b,
c,
d,
e,
f,
g,
h
)
}
}

@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D, E, F, G, H, I, J> Result<A>.zip(
b: Result<B>,
c: Result<C>,
Expand All @@ -182,10 +260,29 @@ public inline fun <A, B, C, D, E, F, G, H, I, J> Result<A>.zip(
transform: (A, B, C, D, E, F, G, H, I) -> J
): Result<J> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return 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) }
return 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")
@Deprecated(
"Prefer using the inline result DSL",
ReplaceWith(
"result {transform(this.bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind(), k.bind()) }",
"arrow.core.raise.result"
)
)
public inline fun <A, B, C, D, E, F, G, H, I, J, K> Result<A>.zip(
b: Result<B>,
c: Result<C>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package arrow.core

import arrow.core.raise.result
import arrow.core.test.result
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.checkAll

class ResultTest : StringSpec({

"null zip null" {
val x = Result.success<Int?>(null)
x.zip(x) { y, z ->
(y?.plus(z ?: -2)) ?: -1
} shouldBe Result.success(-1)
}


"result DSL + bind usage should return the same as deprecated zip" {
checkAll(Arb.int(), Arb.int(), Arb.int()) { x, y, z ->
val resX = Result.success(x)
val resY = Result.success(y)
val resZ = Result.success(z)
val zip = resX.zip(resY, resZ) { a, b, c -> a + b + c }
val dsl = result { resX.bind() + resY.bind() + resZ.bind() }
dsl shouldBe zip
}
}

})