Ref

object Ref
Companion
class
class Object
trait Matchable
class Any

Type members

Classlikes

@implicitNotFound("Cannot find an instance for Ref.Make. Add implicit evidence of Concurrent[${F}, _] or Sync[${F}] to scope to automatically derive one.")
trait Make[F <: ([_$4] =>> Any)]
Companion
object
object Make extends MakeInstances
Companion
class
final class ApplyBuilders[F <: ([_$16] =>> Any)](val mk: Make[F]) extends AnyVal

Value members

Methods

def apply[F <: ([_$7] =>> Any)](mk: Make[F]): ApplyBuilders[F]
Builds a Ref value for data types that are Sync
This builder uses the
Partially-Applied Type
technique.
{{{
Ref[IO] .of(10) <-> Ref.ofIO, Int
}}}
See also
def of[F <: ([_$8] =>> Any), A](a: A)(mk: Make[F]): F[Ref[F, A]]
Creates an asynchronous, concurrent mutable reference initialized to the supplied value.
{{{
import cats.effect.IO
import cats.effect.kernel.Ref
for {
intRef <- Ref.ofIO, Int
ten <- intRef.get
} yield ten
}}}
def copyOf[F <: ([_$9] =>> Any), A](source: Ref[F, A])(evidence$1: Make[F], evidence$2: FlatMap[F]): F[Ref[F, A]]
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).
def ofEffect[F <: ([_$10] =>> Any), A](fa: F[A])(evidence$3: Make[F], evidence$4: FlatMap[F]): F[Ref[F, A]]
Creates a Ref starting with the result of the effect fa.
def unsafe[F <: ([_$12] =>> Any), A](a: A)(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.
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.unsafeIO, Int
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] = RefIO.map(new Counter(_))
}
}}}
def in[F <: ([_$13] =>> Any), G <: ([_$14] =>> Any), A](a: A)(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
def lens[F <: ([_$15] =>> Any), A, B <: AnyRef](ref: Ref[F, A])(get: A => B, set: A => B => A)(F: Sync[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.
Example:
{{{
case class Foo(bar: String, baz: Int)
val refA: Ref[IO, Foo] = ???
val refB: Ref[IO, String] =
Ref.lensIO, Foo, String(_.bar, (foo: Foo) => (bar: String) => foo.copy(bar = bar))
}}}

Implicits

Implicits

implicit def catsInvariantForRef[F <: ([_$29] =>> Any)](evidence$5: Functor[F]): Invariant[[_$30] =>> Ref[F, _$30]]