//arrow-fx-coroutines/arrow.fx.coroutines/Use

Use

common 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

   
Use common fun <A> Use(acquire: suspend () -> A)

Extensions

Name Summary
release common infix fun <A> Use<A>.release(release: suspend (A) -> Unit): Resource<A>
releaseCase common infix fun <A> Use<A>.releaseCase(release: suspend (A, ExitCase) -> Unit): Resource<A>

Do you like Arrow?

Arrow Org
<