Use

inline class Use<A>(acquire: suspend () -> A)

Marker for suspend () -> A to be marked as the Use action of a Resource. Offers a convenient DSL to use Resource for simple resources.

import arrow.fx.coroutines.*

class File(url: String) {
suspend fun open(): File = this
suspend fun close(): Unit {}
override fun toString(): String = "This file contains some interesting content!"
}

suspend fun openFile(uri: String): File = File(uri).open()
suspend fun closeFile(file: File): Unit = file.close()
suspend fun fileToString(file: File): String = file.toString()

suspend fun main(): Unit {
val res = resource {
openFile("data.json")
} release { file ->
closeFile(file)
} use { file ->
fileToString(file)
}

println(res)
}

Constructors

Link copied to clipboard
fun <A> Use(acquire: suspend () -> A)

Extensions

Link copied to clipboard
infix fun <A> Use<A>.release(release: suspend (A) -> Unit): Resource<A>
Link copied to clipboard
infix fun <A> Use<A>.releaseCase(release: suspend (A, ExitCase) -> Unit): Resource<A>