arrow-fx / arrow.fx / Resource / invoke

invoke

abstract operator fun <C> invoke(use: (A) -> Kind<F, C>): Kind<F, C>

Use the created resource When done will run all finalizers

import arrow.fx.IO
import arrow.fx.Resource
import arrow.fx.extensions.io.bracket.bracket
import arrow.fx.fix

fun acquireResource(): IO<Int> = IO { println("Getting expensive resource"); 42 }
fun releaseResource(r: Int): IO<Unit> = IO { println("Releasing expensive resource: $r") }

fun main() {
  //sampleStart
  val program = Resource(::acquireResource, ::releaseResource, IO.bracket()).invoke {
    IO { println("Expensive resource under use! $it") }
  }
  //sampleEnd
  program.fix().unsafeRunSync()
}

operator fun <F, E, A> invoke(acquire: () -> Kind<F, A>, release: (A, ExitCase<E>) -> Kind<F, Unit>, BR: Bracket<F, E>): Resource<F, E, A>

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

import arrow.fx.IO
import arrow.fx.Resource
import arrow.fx.extensions.io.bracket.bracket
import arrow.fx.fix

fun acquireResource(): IO<Int> = IO { println("Getting expensive resource"); 42 }
fun releaseResource(r: Int): IO<Unit> = IO { println("Releasing expensive resource: $r") }

fun main() {
  //sampleStart
  val resource = Resource(::acquireResource, ::releaseResource, IO.bracket())
  //sampleEnd
  resource.invoke {
    IO { println("Expensive resource under use! $it") }
  }.fix().unsafeRunSync()
}

operator fun <F, E, A> invoke(acquire: () -> Kind<F, A>, release: (A) -> Kind<F, Unit>, BR: Bracket<F, E>): Resource<F, E, A>

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

See Also

invoke

Do you like Arrow?

Arrow Org
<