Context

zio.blocks.context.Context
See theContext companion object
final class Context[+R] extends Serializable

A type-indexed heterogeneous collection that stores values indexed by their types.

Context provides compile-time type safety for storing and retrieving values of different types. The phantom type parameter R tracks which types are present in the context, enabling the type system to verify that requested values exist.

Type parameters

R

The phantom type representing the types stored in this context. Uses intersection types (A & B) to represent multiple stored types.

Attributes

Companion
object
Source
Context.scala
Graph
Supertypes
trait Serializable
class Object
trait Matchable
class Any
Self type
Context[R]

Members list

Value members

Concrete methods

def ++[R1](that: Context[R1]): Context[R & R1]

Combines this context with another, returning a new context containing all entries.

Combines this context with another, returning a new context containing all entries.

When both contexts contain the same type, the value from that (right) wins.

Type parameters

R1

the type parameter of the other context

Value parameters

that

the context to merge with

Attributes

Returns

a new context containing entries from both

Source
Context.scala
def add[A](a: A)(implicit ev: IsNominalType[A]): Context[R & A]

Adds a value to this context, returning a new context with the expanded type.

Adds a value to this context, returning a new context with the expanded type.

If a value of the same type already exists, it is replaced.

Type parameters

A

the type of the value to add

Value parameters

a

the value to add (must not be null)

Attributes

Returns

a new context containing the added value

Throws
Source
Context.scala
def get[A >: R](implicit ev: IsNominalType[A]): A

Retrieves the value of type A from this context.

Retrieves the value of type A from this context.

The type bound A >: R ensures at compile time that a value of type A (or a subtype) is present. Supertype lookups are supported: if you store a Dog and request an Animal, the Dog will be returned.

Type parameters

A

the type to retrieve (must be a supertype of some type in R)

Attributes

Returns

the stored value

Throws
scala.NoSuchElementException

if no matching value is found (indicates a bug)

Source
Context.scala
def getOption[A](implicit ev: IsNominalType[A]): Option[A]

Retrieves the value of type A if present, without requiring a type bound.

Retrieves the value of type A if present, without requiring a type bound.

Unlike get, this method does not require A to be in the context's type parameter. Use this for optional lookups where the type may or may not be present.

Type parameters

A

the type to retrieve

Attributes

Returns

Some(value) if found, None otherwise

Source
Context.scala
def isEmpty: Boolean

Returns true if this context contains no entries.

Returns true if this context contains no entries.

Attributes

Source
Context.scala
def nonEmpty: Boolean

Returns true if this context contains at least one entry.

Returns true if this context contains at least one entry.

Attributes

Source
Context.scala
def prune[A >: R](implicit ev: IsNominalIntersection[A]): Context[A]

Narrows this context to contain only the specified types.

Narrows this context to contain only the specified types.

The type A should be an intersection of nominal types that are present in R. Any entries not in A are removed.

Type parameters

A

the intersection type to keep (must be a supertype of R)

Attributes

Returns

a new context containing only entries for types in A

Source
Context.scala
def size: Int

Returns the number of entries in this context.

Returns the number of entries in this context.

Attributes

Source
Context.scala
override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any
Source
Context.scala
def update[A >: R](f: A => A)(implicit ev: IsNominalType[A]): Context[R]

Transforms an existing value in this context.

Transforms an existing value in this context.

If the type is not present, the context is returned unchanged.

Type parameters

A

the type to update (must be a supertype of some type in R)

Value parameters

f

the transformation function (must not return null)

Attributes

Returns

a new context with the transformed value

Throws
java.lang.NullPointerException

if f returns null

Source
Context.scala