Resource

zio.blocks.scope.Resource
See theResource companion object
sealed trait Resource[+A]

A description of how to acquire and release a resource.

Resources encapsulate both acquisition and finalization, tying a value's lifecycle to a Scope. When a resource is created via scope.allocate(resource), the scope registers any finalizers and ensures they run when the scope closes.

Resources are lazy—they describe ''what'' to do, not ''when''. Creation only happens when passed to a scope via scope.allocate(resource).

==Creating Resources==

  • Resource(=> a): By-name value; auto-registers close() if AutoCloseable
  • Resource.acquireRelease(acquire)(release): Explicit acquire/release
  • Resource.fromAutoCloseable(=> a): For AutoCloseable subtypes
  • Resource.from[T] macro: Derives from constructor

Type parameters

A

the type of value this resource produces

Attributes

See also

[[Scope.allocateA* Scope.allocate]] for using resources

Wire.toResource for converting wires

Example
 Scope.global.scoped { scope =>
   import scope._
   val db = allocate(Resource.fromAutoCloseable(new Database()))
   (scope $ db)(_.query("SELECT 1"))
 }
Companion
object
Source
Resource.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def flatMap[B](f: A => Resource[B]): Resource[B]

Sequences two resources, using the result of this resource to create another.

Sequences two resources, using the result of this resource to create another.

Both resources' finalizers are registered: the inner resource's finalizers run before the outer resource's finalizers (LIFO order).

Type parameters

B

the type produced by the resulting resource

Value parameters

f

a function that produces a resource from the value of this resource

Attributes

Returns

a new resource combining both acquisitions

Example
 val configResource: Resource[Config] = Resource(loadConfig())
 val dbResource: Resource[Database] = configResource.flatMap { config =>
   Resource.fromAutoCloseable(new Database(config.url))
 }
Source
Resource.scala
def map[B](f: A => B): Resource[B]

Transforms the value produced by this resource.

Transforms the value produced by this resource.

The transformation function f is applied after the resource is acquired. Finalizers from the original resource still run when the scope closes.

Type parameters

B

the result type

Value parameters

f

the transformation function

Attributes

Returns

a new resource that produces transformed values

Example
 val portResource: Resource[Int] = Resource(8080)
 val urlResource: Resource[String] = portResource.map(port => s"http://localhost:$$port")
Source
Resource.scala
final def use[B](f: A => B)(implicit ev: Unscoped[B]): B

Acquires this resource in Scope.global, uses it, and closes it when the callback returns.

Acquires this resource in Scope.global, uses it, and closes it when the callback returns.

This provides a lighter-weight entrypoint for application code while preserving the same finalization semantics as explicit Scope.global.scoped { scope => ... } usage.

Type parameters

B

result type; must be Unscoped so only pure data escapes the scope

Value parameters

f

callback that uses the acquired value

Attributes

Returns

the callback result after all finalizers have run

Example
 val serverResource: Resource[HttpServer] = ???
 serverResource.use(_.start())
Source
Resource.scala
def zip[B](that: Resource[B]): Resource[(A, B)]

Combines this resource with another, producing a tuple of both values.

Combines this resource with another, producing a tuple of both values.

Both resources are acquired, and both sets of finalizers are registered. Finalizers run in LIFO order (second resource's finalizers before first).

Type parameters

B

the type produced by the other resource

Value parameters

that

the resource to combine with

Attributes

Returns

a new resource producing a tuple of both values

Example
 val dbResource: Resource[Database] = Resource.fromAutoCloseable(new Database())
 val cacheResource: Resource[Cache] = Resource.fromAutoCloseable(new Cache())
 val combined: Resource[(Database, Cache)] = dbResource.zip(cacheResource)
Source
Resource.scala