//arrow-fx-coroutines/arrow.fx.coroutines/Resource/Companion/invoke

invoke

common operator fun <A> invoke(acquire: suspend () -> A, release: suspend (A, ExitCase) -> Unit): Resource<A>

Construct a Resource from an allocating function acquire and a release function release.

import arrow.fx.coroutines.*

suspend fun acquireResource(): Int = 42.also { println("Getting expensive resource") }
suspend fun releaseResource(r: Int, exitCase: ExitCase): Unit = println("Releasing expensive resource: $r, exit: $exitCase")

suspend fun main(): Unit {
  //sampleStart
  val resource = Resource(::acquireResource, ::releaseResource)
  resource.use {
    println("Expensive resource under use! $it")
  }
  //sampleEnd
}

Do you like Arrow?

Arrow Org
<