allocated
@DelicateCoroutinesApi
Content copied to clipboard
Deconstruct Resource into an acquire and release handlers. The release action must always be called with resource A returned from acquire, if the release step is never called, then the resource A will leak. The acquire and release steps are already made NonCancellable to guarantee correct invocation like Resource or bracketCase.
import arrow.fx.coroutines.*
import arrow.fx.coroutines.ExitCase.Companion.ExitCase
val resource = Resource({ println("Acquire") }) { _, exitCase ->
println("Release $exitCase")
}
suspend fun main(): Unit {
val (acquire, release) = resource.allocated()
val a = acquire()
try {
/** Do something with A */
release(a, ExitCase.Completed)
} catch(e: Throwable) {
val e2 = runCatching { release(a, ExitCase(e)) }.exceptionOrNull()
throw Platform.composeErrors(e, e2)
}
}Content copied to clipboard
This is a delicate API. It is easy to accidentally create resource or memory leaks allocated is used. A Resource allocated by allocated 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.