Ref

cats.effect.kernel.Ref$
See theRef companion class
object Ref

Attributes

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

Members list

Type members

Classlikes

final class ApplyBuilders[F[_]](val mk: Make[F]) extends AnyVal

Attributes

Source
Ref.scala
Supertypes
class AnyVal
trait Matchable
class Any
object Make

Attributes

Companion
trait
Source
Ref.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Make.type
trait Make[F[_]]

Attributes

Companion
object
Source
Ref.scala
Supertypes
class Object
trait Matchable
class Any

Value members

Concrete methods

def apply[F[_]](implicit mk: Make[F]): ApplyBuilders[F]

Builds a Ref value for data types that are Sync

Builds a Ref value for data types that are Sync

This builder uses the Partially-Applied Type technique.

 Ref[IO].of(10) <-> Ref.of[IO, Int](10)

Attributes

See also

of

Source
Ref.scala
def copyOf[F[_] : FlatMap, A](source: Ref[F, A]): F[Ref[F, A]]

Creates a Ref starting with the value of the one in source.

Creates a Ref starting with the value of the one in source.

Updates of either of the Refs will not have an effect on the other (assuming A is immutable).

Attributes

Source
Ref.scala
def empty[F[_] : Make, A : Monoid]: F[Ref[F, A]]

Creates a Ref with empty content

Creates a Ref with empty content

Attributes

Source
Ref.scala
def in[F[_], G[_], A](a: A)(implicit F: Sync[F], G: Sync[G]): F[Ref[G, A]]

Builds a Ref value for data types that are Sync like of but initializes state using another effect constructor

Builds a Ref value for data types that are Sync like of but initializes state using another effect constructor

Attributes

Source
Ref.scala
def lens[F[_], A, B](ref: Ref[F, A])(get: A => B, set: A => B => A)(implicit F: Functor[F]): Ref[F, B]

Creates an instance focused on a component of another Ref's value. Delegates every get and modification to underlying Ref, so both instances are always in sync.

Creates an instance focused on a component of another Ref's value. Delegates every get and modification to underlying Ref, so both instances are always in sync.

Example:

 case class Foo(bar: String, baz: Int)

 val refA: Ref[IO, Foo] = ???
 val refB: Ref[IO, String] =
   Ref.lens[IO, Foo, String](refA)(_.bar, (foo: Foo) => (bar: String) => foo.copy(bar = bar))

Attributes

Source
Ref.scala
def of[F[_], A](a: A)(implicit mk: Make[F]): F[Ref[F, A]]

Creates a thread-safe, concurrent mutable reference initialized to the supplied value.

Creates a thread-safe, concurrent mutable reference initialized to the supplied value.

 import cats.effect.IO
 import cats.effect.kernel.Ref

 for {
   intRef <- Ref.of[IO, Int](10)
   ten <- intRef.get
 } yield ten

Attributes

Source
Ref.scala
def ofEffect[F[_] : FlatMap, A](fa: F[A]): F[Ref[F, A]]

Creates a Ref starting with the result of the effect fa.

Creates a Ref starting with the result of the effect fa.

Attributes

Source
Ref.scala
def unsafe[F[_], A](a: A)(implicit F: Sync[F]): Ref[F, A]

Like apply but returns the newly allocated Ref directly instead of wrapping it in F.delay. This method is considered unsafe because it is not referentially transparent -- it allocates mutable state.

Like apply but returns the newly allocated Ref directly instead of wrapping it in F.delay. This method is considered unsafe because it is not referentially transparent -- it allocates mutable state.

This method uses the Partially Applied Type Params technique, so only effect type needs to be specified explicitly.

Some care must be taken to preserve referential transparency:

 import cats.effect.IO
 import cats.effect.kernel.Ref

 class Counter private () {
   private val count = Ref.unsafe[IO, Int](0)

   def increment: IO[Unit] = count.update(_ + 1)
   def total: IO[Int] = count.get
 }

 object Counter {
   def apply(): IO[Counter] = IO(new Counter)
 }

Such usage is safe, as long as the class constructor is not accessible and the public one suspends creation in IO.

The recommended alternative is accepting a Ref[F, A] as a parameter:

 class Counter (count: Ref[IO, Int]) {
   // same body
 }

 object Counter {
   def apply(): IO[Counter] = Ref[IO](0).map(new Counter(_))
 }

Attributes

Source
Ref.scala

Deprecated methods

def lens[F[_], A, B <: AnyRef](ref: Ref[F, A], get: A => B, set: A => B => A, F: Sync[F]): Ref[F, B]

Attributes

Deprecated
true
Source
Ref.scala

Implicits

Implicits

implicit def catsInvariantForRef[F[_] : Functor]: Invariant[[_] =>> Ref[F, _$32]]

Attributes

Source
Ref.scala