Norbi
04/08/2024, 8:27 PMpublic inline fun <Error, A, B> fold(
@BuilderInference block: Raise<Error>.() -> A,
catch: (throwable: Throwable) -> B,
recover: (error: Error) -> B,
transform: (value: A) -> B,
): B
method has the following remark:
This method should never be wrapped in try/catch as it will not throw any unexpected errorsActually, if the
recover
handler throws an exception it is simply rethrown, so the catch
handler applies only to block
, so the following test succeeds:
data object DivisionByZero
context(Raise<DivisionByZero>)
private fun div(a: Int, b: Int): Int =
if (b != 0) {
a / b
} else {
raise(DivisionByZero)
}
@Test
fun testFoldExceptionHandling() {
try {
fold<DivisionByZero, Int, Int>({
div(1, 0)
}, {
throw IllegalStateException("catch", it)
}, {
throw IllegalStateException("recover: $it")
},
::identity
)
fail()
} catch (e: Exception) {
assertEquals("recover: $DivisionByZero", e.message)
}
}
My questions is: is the documentation stale, or is this an actual semantic bug?Norbi
04/08/2024, 8:28 PMrecover()
as well:
@Test
fun testRecoverExceptionHandling() {
try {
recover<DivisionByZero, Int>({
div(1, 0)
}, {
throw IllegalStateException("recover: $it")
}, {
throw IllegalStateException("catch", it)
})
fail()
} catch (e: Exception) {
assertEquals("recover: $DivisionByZero", e.message)
}
}
Youssef Shoaib [MOD]
04/08/2024, 8:34 PM