Should `arrow.core.raise.fold` declare `callsInPla...
# arrow
p
Should
arrow.core.raise.fold
declare
callsInPlace(block, EXACTLY_ONCE)
? - edit: short answer is no
c
Can you clarify which
fold
overload you mean? There are a few... https://apidocs.arrow-kt.io/arrow-core/arrow.core.raise/fold.html
If you think the one with
block
,
recover
and
transform
, then I think so, yes 🤔
p
Coming from arrow 1.2.4, all of the non-extension
fold
overloads that take a
block: Raise<Error>.() -> A
seem to not specify
callsInPlace
for
block
- however I see in 2.x some of them now do (but declare it as AT_MOST_ONCE despite it seemingly always being called
ah - because block is called inside a try/catch it's only
AT_MOST_ONCE
as it may not complete, despite the surrounding function call completing
e.g.
Copy code
fun main() {
  val s: String
  fold<Unit, _, _>({
    ensure(false) {}
    s = "hello"
  }, { throw it }, {}, {})
  println(s.length)
}
this would compile with
EXACTLY_ONCE
but (correctly) fail to compile with
AT_MOST_ONCE
c
Wait, so
EXACTLY_ONCE
can only be used if it runs to completion? But how can you know that in the general case, exceptions could be thrown anywhere?
p
as I (now) understand it,
EXACTLY_ONCE
means "if the outer function completes, the inner function was invoked and completed exactly once"
https://pl.kotl.in/uCq7XC2ce this example will compile and run but throw an unexpected NPE and compilation will give the warning (with Kotlin 2.0.0) of:
Wrong invocation kind 'EXACTLY_ONCE' for 'block: () -> Unit' specified, the actual invocation kind is 'AT_MOST_ONCE'.