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

Raise DSL for Ior does not respect recover guarantees in the same way other types do #3051

Closed
yoxjames opened this issue May 9, 2023 · 1 comment

Comments

@yoxjames
Copy link
Contributor

yoxjames commented May 9, 2023

Recover can be successfully when using the Raise DSL to construct an Either<A, B> but does not behave the same way when constructing an Ior<A,B> see the two tests below.

@Test
fun eitherAttempt() = runTest {
    val success: Either<String, Int> = 1.right()
    val failure: Either<String, Int> = "fail".left()

    val actual = either {
        val test = recover({ failure.bind() }) { 2 }
        "${success.bind()} : $test"
    }

    assertEquals(
        expected = "1 : 2".right(),
        actual = actual
    )
}

This is successful. recover({..}) { ... } allows us to constrain the the raise (shift) in bind(). However when we try this for Ior.

@Test
fun iorAttempt() = runTest {
    val greatSuccess: Ior<Nel<String>, Int> = 1.rightIor()
    val semiSuccess: Ior<Nel<String>, Int> = Ior.Both("little fail".nel(), 2)
    val epicFailure: Ior<Nel<String>, Int> = "epic fail".nel().leftIor()

    val actual = ior(combineError = { a, b -> a + b }) {
        val test = recover<Nel<String>, Int>({ epicFailure.bind() }) { 3 }
        "${greatSuccess.bind()} : ${semiSuccess.bind()} : $test"
    }

    assertEquals(
        expected = Ior.Both(nonEmptyListOf("little fail", "big fail"), "1 : 2 : 3"),
        actual = actual
    ) // This fails!
}

This test fails. We actually get

expected:<Ior.Both(NonEmptyList(little fail, big fail), 1 : 2 : 3)> but was:<Ior.Left(NonEmptyList(epic fail))>
Expected :Ior.Both(NonEmptyList(little fail, big fail), 1 : 2 : 3)
Actual   :Ior.Left(NonEmptyList(epic fail))

The bind() is inside a recover but still short circuits out of the parent Raise block (ior { ... })

See the slack convo from the kotlinlang slack with original findings.
https://kotlinlang.slack.com/archives/C5UPMM0A0/p1683401532401899

This affects 1.20-RC as well as 1.1.5.

@yoxjames
Copy link
Contributor Author

yoxjames commented Jun 1, 2023

Issue has been resolved by PR #3052

@yoxjames yoxjames closed this as completed Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant