Resource

zio.blocks.scope.Resource
See theResource companion trait
object Resource

Attributes

Companion
trait
Source
Resource.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Resource.type

Members list

Value members

Concrete methods

def acquireRelease[A](acquire: => A)(release: A => Unit): Resource[A]

Creates a resource with explicit acquire and release functions.

Creates a resource with explicit acquire and release functions.

The acquire thunk is called when the resource is created. The release function is registered as a finalizer and runs when the scope closes.

Type parameters

A

the resource type

Value parameters

acquire

a by-name expression that acquires the resource

release

a function that releases the resource

Attributes

Returns

a unique resource with explicit lifecycle management

Example
 val fileResource = Resource.acquireRelease {
   new FileInputStream("data.txt")
 } { stream =>
   stream.close()
 }
Source
Resource.scala
def apply[A](value: => A): Resource[A]

Creates a resource from a by-name value.

Creates a resource from a by-name value.

If the value implements AutoCloseable, its close() method is automatically registered as a finalizer. For non-closeable values, no finalization is performed.

Type parameters

A

the value type

Value parameters

value

a by-name expression that produces the resource value

Attributes

Returns

a unique resource that acquires the value and auto-closes if applicable

Example
 // AutoCloseable is auto-finalized
 val dbResource = Resource(new Database())
 // Non-closeable is just wrapped
 val configResource = Resource(Config("localhost", 8080))
Source
Resource.scala
def fromAutoCloseable[A <: AutoCloseable](thunk: => A): Resource[A]

Creates a resource from an AutoCloseable value.

Creates a resource from an AutoCloseable value.

The value's close() method is registered as a finalizer. This is type-safe alternative to Resource(value) when you know the value is AutoCloseable at compile time.

Type parameters

A

the AutoCloseable subtype

Value parameters

thunk

a by-name expression that produces the AutoCloseable value

Attributes

Returns

a unique resource that closes the value when the scope closes

Example
 val streamResource = Resource.fromAutoCloseable {
   new BufferedInputStream(new FileInputStream("data.bin"))
 }
Source
Resource.scala
def shared[A](f: Scope => A): Resource[A]

Creates a shared resource from a function.

Creates a shared resource from a function.

Shared resources are memoized: the first call initializes the value, subsequent calls return the same instance with reference counting. Finalizers run when the last reference is released.

Type parameters

A

the value type

Value parameters

f

a function from scope to value

Attributes

Returns

a shared resource

Source
Resource.scala
def unique[A](f: Scope => A): Resource[A]

Creates a unique resource from a function.

Creates a unique resource from a function.

Unique resources create a fresh instance each time they are allocated.

Type parameters

A

the value type

Value parameters

f

a function from scope to value

Attributes

Returns

a unique resource

Source
Resource.scala

Inherited methods

inline def from[T](inline wires: Wire[_, _]*): Resource[T]

Derives a Resource[T] from T's constructor with wire overrides for dependencies.

Derives a Resource[T] from T's constructor with wire overrides for dependencies.

The provided wires act as overrides for constructor dependencies. Any remaining dependencies are derived automatically when possible. If a dependency cannot be satisfied (no override and not derivable), a compile-time error is produced.

This is useful when you want to create a standalone resource that fully encapsulates its dependency graph.

Type parameters

T

the type to construct

Value parameters

wires

wires that provide all required dependencies

Attributes

Returns

a resource that creates T instances

Example
 class Service(db: Database, config: Config)
 // Provide wires for all dependencies
 val resource = Resource.from[Service](
   Wire.shared[Database],
   Wire(Config("localhost", 8080))
 )
Inherited from:
ResourceCompanionVersionSpecific (hidden)
Source
ResourceVersionSpecific.scala
inline def from[T]: Resource[T]

Derives a Resource[T] from T's constructor.

Derives a Resource[T] from T's constructor.

Only works for types with no dependencies. If T has constructor parameters (other than constructor parameters of type Scope/Finalizer), use Wire[T] and call .toResource(deps).

If T extends AutoCloseable, its close() method is automatically registered as a finalizer.

Type parameters

T

the type to construct (must be a class with no dependencies)

Attributes

Returns

a resource that creates T instances

Inherited from:
ResourceCompanionVersionSpecific (hidden)
Source
ResourceVersionSpecific.scala