autoCloseable
suspend fun <A : AutoCloseable> ResourceScope.autoCloseable(closingDispatcher: CoroutineDispatcher = IODispatcher, autoCloseable: suspend () -> A): A
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
Content copied to clipboard
fun <A : AutoCloseable> autoCloseable(closingDispatcher: CoroutineDispatcher = IODispatcher, autoCloseable: suspend () -> A): Resource<A>