autoCloseable

suspend fun <A : AutoCloseable> ResourceScope.autoCloseable(closingDispatcher: CoroutineDispatcher = Dispatchers.IO, autoCloseable: suspend () -> A): A(source)

Creates a Resource from an AutoCloseable, which uses AutoCloseable.close for releasing.

import arrow.fx.coroutines.resourceScope
import arrow.fx.coroutines.autoCloseable
import java.io.FileInputStream

suspend fun copyFile(src: String, dest: String): Unit =
resourceScope {
val a: FileInputStream = autoCloseable { FileInputStream(src) }
val b: FileInputStream = autoCloseable { FileInputStream(dest) }
/** read from [a] and write to [b]. **/
} // Both resources will be closed accordingly to their #close methods

fun <A : AutoCloseable> autoCloseable(closingDispatcher: CoroutineDispatcher = Dispatchers.IO, autoCloseable: suspend () -> A): Resource<A>(source)