//arrow-fx-coroutines/arrow.fx.coroutines/Resource/use

use

common infix suspend tailrec fun <B> use(f: suspend (A) -> B): B

Use the created resource When done will run all finalizers

import arrow.fx.coroutines.*

class DataSource {
  fun connect(): Unit = println("Connecting dataSource")
  fun users(): List<String> = listOf("User-1", "User-2", "User-3")
  fun close(): Unit = println("Closed dataSource")
}

suspend fun main(): Unit {
  //sampleStart
  val dataSource = resource {
    DataSource().also { it.connect() }
  } release DataSource::close

  val res = dataSource
    .use { ds -> "Using data source: ${ds.users()}" }
    .also(::println)
  //sampleEnd
}

Do you like Arrow?

Arrow Org
<