FiberRef

final class FiberRef[A] extends Serializable

Fiber's counterpart for Java's ThreadLocal. Value is automatically propagated to child on fork and merged back in after joining child.

for {
 fiberRef <- FiberRef.make("Hello world!")
 child <- fiberRef.set("Hi!).fork
 result <- child.join
} yield result

result will be equal to "Hi!" as changes done by child were merged on join.

FiberRef#make also allows specifying how the values will be combined when joining. By default this will use the value of the joined fiber.

for {
 fiberRef <- FiberRef.make(0, math.max)
 child    <- fiberRef.update(_ + 1).fork
 _        <- fiberRef.update(_ + 2)
 _        <- child.join
 value    <- fiberRef.get
} yield value

value will be 2 as the value in the joined fiber is lower and we specified max as our combine function.

Companion:
object
class Object
trait Matchable
class Any

Value members

Concrete methods

def getAndSet(a: A): UIO[A]

Atomically sets the value associated with the current fiber and returns the old value.

Atomically sets the value associated with the current fiber and returns the old value.

def getAndUpdate(f: A => A): UIO[A]

Atomically modifies the FiberRef with the specified function and returns the old value.

Atomically modifies the FiberRef with the specified function and returns the old value.

Atomically modifies the FiberRef with the specified partial function and returns the old value. If the function is undefined on the current value it doesn't change it.

Atomically modifies the FiberRef with the specified partial function and returns the old value. If the function is undefined on the current value it doesn't change it.

def locally[R, E, B](value: A)(use: ZIO[R, E, B]): ZIO[R, E, B]

Returns an IO that runs with value bound to the current fiber.

Returns an IO that runs with value bound to the current fiber.

Guarantees that fiber data is properly restored via bracket.

def modify[B](f: A => (B, A)): UIO[B]

Atomically modifies the FiberRef with the specified function, which computes a return value for the modification. This is a more powerful version of update.

Atomically modifies the FiberRef with the specified function, which computes a return value for the modification. This is a more powerful version of update.

def modifySome[B](default: B)(pf: PartialFunction[A, (B, A)]): UIO[B]

Atomically modifies the FiberRef with the specified partial function, which computes a return value for the modification if the function is defined in the current value otherwise it returns a default value. This is a more powerful version of updateSome.

Atomically modifies the FiberRef with the specified partial function, which computes a return value for the modification if the function is defined in the current value otherwise it returns a default value. This is a more powerful version of updateSome.

def set(value: A): UIO[Unit]

Sets the value associated with the current fiber.

Sets the value associated with the current fiber.

Returns a ThreadLocal that can be used to interact with this FiberRef from side effecting code.

Returns a ThreadLocal that can be used to interact with this FiberRef from side effecting code.

This feature is meant to be used for integration with side effecting code, that needs to access fiber specific data, like MDC contexts and the like. The returned ThreadLocal will be backed by this FiberRef on all threads that are currently managed by ZIO, and behave like an ordinary ThreadLocal on all other threads.

def update(f: A => A): UIO[Unit]

Atomically modifies the FiberRef with the specified function.

Atomically modifies the FiberRef with the specified function.

def updateAndGet(f: A => A): UIO[A]

Atomically modifies the FiberRef with the specified function and returns the result.

Atomically modifies the FiberRef with the specified function and returns the result.

def updateLocally[R, E, B](f: A => A)(use: ZIO[R, E, B]): ZIO[R, E, B]

Returns an IO that runs with result of calling the specified function bound to the current fiber.

Returns an IO that runs with result of calling the specified function bound to the current fiber.

Guarantees that fiber data is properly restored via bracket.

Atomically modifies the FiberRef with the specified partial function. If the function is undefined on the current value it doesn't change it.

Atomically modifies the FiberRef with the specified partial function. If the function is undefined on the current value it doesn't change it.

Atomically modifies the FiberRef with the specified partial function. If the function is undefined on the current value it returns the old value without changing it.

Atomically modifies the FiberRef with the specified partial function. If the function is undefined on the current value it returns the old value without changing it.

def updateSomeLocally[R, E, B](pf: PartialFunction[A, A])(use: ZIO[R, E, B]): ZIO[R, E, B]

Returns an IO that runs with result of calling the specified partial function bound to the current fiber.

Returns an IO that runs with result of calling the specified partial function bound to the current fiber.

Guarantees that fiber data is properly restored via bracket.

Concrete fields

val get: UIO[A]

Reads the value associated with the current fiber. Returns initial value if no value was set or inherited from parent.

Reads the value associated with the current fiber. Returns initial value if no value was set or inherited from parent.