Wire

zio.blocks.scope.Wire
See theWire companion object
sealed trait Wire[-In, +Out]

A recipe for constructing a service and its dependencies.

Wires are the building blocks of dependency injection in Scope. They describe how to construct a service given access to its dependencies via a context.

==Wire Types==

==Creating Wires==

Use Wire.shared[T] and Wire.unique[T] macros for automatic derivation from constructors, or create wires manually for custom construction logic.

==Usage Example==

// Define a service with its wire
class Database(config: Config)
object Database {
 val wire: Wire[Config, Database] = Wire.shared[Database]
}

// Create a unique wire for request-scoped services
class RequestHandler(db: Database)
object RequestHandler {
 val wire: Wire[Database, RequestHandler] = Wire.unique[RequestHandler]
}

// Create a Resource from wires
val appResource = Resource.from[App](
 Wire(Config("localhost", 8080)),
 Wire.shared[Database]
)

Type parameters

In

the dependencies required (contravariant - accepts supertypes)

Out

the service(s) produced (covariant - produces subtypes)

Attributes

Companion
object
Source
Wire.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Shared[In, Out]
class Unique[In, Out]

Members list

Value members

Abstract methods

def isShared: Boolean

Returns true if this wire is shared (reference-counted via Resource.shared).

Returns true if this wire is shared (reference-counted via Resource.shared).

Shared wires are converted to Resource.shared, meaning allocations may share a single underlying instance across scopes. The instance is released when the last referencing scope is closed.

Attributes

Returns

true if this wire is shared, false if unique

Source
Wire.scala
def shared: Shared[In, Out]

Converts this wire to a shared wire.

Converts this wire to a shared wire.

If already shared, returns this. Otherwise, creates a new Wire.Shared wire with the same construction function.

Attributes

Returns

a shared version of this wire

Source
Wire.scala
def toResource(deps: Context[In]): Resource[Out]

Converts this Wire to a Resource by providing resolved dependencies.

Converts this Wire to a Resource by providing resolved dependencies.

Value parameters

deps

Context containing all required dependencies

Attributes

Returns

a Resource that creates Out values

Source
Wire.scala
def unique: Unique[In, Out]

Converts this wire to a unique wire.

Converts this wire to a unique wire.

If already unique, returns this. Otherwise, creates a new Wire.Unique wire with the same construction function.

Attributes

Returns

a unique version of this wire

Source
Wire.scala

Concrete methods

final def isUnique: Boolean

Returns true if this wire is unique (creates fresh instances each time).

Returns true if this wire is unique (creates fresh instances each time).

Unique wires create a new instance for each dependent service.

Attributes

Returns

true if this wire is unique, false if shared

Source
Wire.scala

Inherited and Abstract methods

def make(scope: Scope, ctx: Context[In]): Out

Constructs the service using the given scope and context.

Constructs the service using the given scope and context.

The scope is used to register finalizers (e.g. for AutoCloseable services), while the context provides access to dependencies required by this wire.

Value parameters

ctx

the context providing dependencies

scope

the scope for cleanup registration

Attributes

Returns

the constructed service

Inherited from:
WireVersionSpecific (hidden)
Source
WireVersionSpecific.scala