final class Ref[A] extends AnyVal with Serializable
A mutable atomic reference for the IO monad. This is the IO equivalent of
a volatile var, augmented with atomic operations, which make it useful as a
reasonably efficient (if low-level) concurrency primitive.
for { ref <- Ref.make(2) v <- ref.update(_ + 3) _ <- console.putStrLn("Value = " + v) // Value = 5 } yield ()
NOTE: While Ref provides the functional equivalent of a mutable reference,
the value inside the Ref should be immutable. For performance reasons
Ref is implemented in terms of compare and swap operations rather than
synchronization. These operations are not safe for mutable values that do
not support concurrent access.
- Alphabetic
- By Inheritance
- Ref
- Serializable
- Serializable
- AnyVal
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
get: UIO[A]
Reads the value from the
Ref.Reads the value from the
Ref.- returns
UIO[A]value from theRef
-
def
getAndSet(a: A): UIO[A]
Atomically writes the specified value to the
Ref, returning the value immediately before modification.Atomically writes the specified value to the
Ref, returning the value immediately before modification. This is not implemented in terms ofmodifypurely for performance reasons.- a
value to be written to the
Ref- returns
UIO[A]value of theRefimmediately before modification
-
def
getAndUpdate(f: (A) ⇒ A): UIO[A]
Atomically modifies the
Refwith the specified function, returning the value immediately before modification.Atomically modifies the
Refwith the specified function, returning the value immediately before modification. This is not implemented in terms ofmodifypurely for performance reasons.- f
function to atomically modify the
Ref- returns
UIO[A]value of theRefimmediately before modification
-
def
getAndUpdateSome(pf: PartialFunction[A, A]): UIO[A]
Atomically modifies the
Refwith the specified partial function, returning the value immediately before modification.Atomically modifies the
Refwith the specified partial function, returning the value immediately before modification. If the function is undefined on the current value it doesn't change it.- pf
partial function to atomically modify the
Ref- returns
UIO[A]value of theRefimmediately before modification
-
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
modify[B](f: (A) ⇒ (B, A)): UIO[B]
Atomically modifies the
Refwith the specified function, which computes a return value for the modification.Atomically modifies the
Refwith the specified function, which computes a return value for the modification. This is a more powerful version ofupdate.- B
type of the value of the
Refto be modified- f
function which computes a return value for the modification
- returns
UIO[B]modified value theRef
-
def
modifySome[B](default: B)(pf: PartialFunction[A, (B, A)]): UIO[B]
Atomically modifies the
Refwith 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.Atomically modifies the
Refwith 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 ofupdateSome.- B
type of the value of the
Refto be modified- default
default value to be returned if the partial function is not defined on the current value
- pf
partial function to be computed on the current value if defined on the current value
- returns
UIO[B]modified value of theRef
-
def
set(a: A): UIO[Unit]
Writes a new value to the
Ref, with a guarantee of immediate consistency (at some cost to performance).Writes a new value to the
Ref, with a guarantee of immediate consistency (at some cost to performance).- a
value to be written to the
Ref- returns
UIO[Unit]
-
def
setAsync(a: A): UIO[Unit]
Writes a new value to the
Refwithout providing a guarantee of immediate consistency.Writes a new value to the
Refwithout providing a guarantee of immediate consistency.- a
value to be written to the
Ref- returns
UIO[Unit]
-
def
toString(): String
- Definition Classes
- Ref → Any
-
def
update(f: (A) ⇒ A): UIO[Unit]
Atomically modifies the
Refwith the specified function.Atomically modifies the
Refwith the specified function. This is not implemented in terms ofmodifypurely for performance reasons.- f
function to atomically modify the
Ref- returns
UIO[Unit]
-
def
updateAndGet(f: (A) ⇒ A): UIO[A]
Atomically modifies the
Refwith the specified function and returns the updated value.Atomically modifies the
Refwith the specified function and returns the updated value. This is not implemented in terms ofmodifypurely for performance reasons.- f
function to atomically modify the
Ref- returns
UIO[A]modified value of theRef
-
def
updateSome(pf: PartialFunction[A, A]): UIO[Unit]
Atomically modifies the
Refwith the specified partial function.Atomically modifies the
Refwith the specified partial function. If the function is undefined on the current value it doesn't change it.- pf
partial function to atomically modify the
Ref- returns
UIO[Unit]
-
def
updateSomeAndGet(pf: PartialFunction[A, A]): UIO[A]
Atomically modifies the
Refwith the specified partial function.Atomically modifies the
Refwith the specified partial function. If the function is undefined on the current value it returns the old value without changing it.- pf
partial function to atomically modify the
Ref- returns
UIO[A]modified value of theRef