allocate

suspend fun allocate(): Pair<A, suspend (ExitCase) -> Unit>(source)

Deconstruct Resource into an A and a release handler. The release action must always be called, if never called, then the resource A will leak. The release step is already made NonCancellable to guarantee correct invocation like Resource or bracketCase, and it will automatically rethrow, and compose, the exceptions as needed.

import arrow.fx.coroutines.*
import arrow.fx.coroutines.ExitCase.Companion.ExitCase

val resource =
resource({ "Acquire" }) { _, exitCase -> println("Release $exitCase") }

suspend fun main(): Unit {
val (acquired: String, release: suspend (ExitCase) -> Unit) = resource.allocate()
try {
/** Do something with A */
release(ExitCase.Completed)
} catch(e: Throwable) {
release(ExitCase(e))
}
}

This is a delicate API. It is easy to accidentally create resource or memory leaks allocated is used. A Resource allocated by allocate is not subject to the guarantees that Resource makes, instead the caller is responsible for correctly invoking the release handler at the appropriate time. This API is useful for building inter-op APIs between Resource and non-suspending code, such as Java libraries.