arrow-fx / arrow.fx / Ref

Ref

interface ~~Ref~~<F, A> Deprecated: The IO datatype and it’s related type classes will disappear in Arrow 0.13.0. All useful operations are offered directly over suspend functions by Arrow Fx Coroutines. https://arrow-kt.io/docs/fx/async/

An asynchronous, concurrent mutable reference.

Provides safe concurrent access and modification of its content. Ref is a purely functional wrapper over an AtomicReference in context F, that is always initialised to a value A.

Functions

access Obtains a snapshot of the current value, and a setter for updating it.abstract fun access(): Kind<F, Tuple2<A, (A) -> Kind<F, Boolean>>>
get Obtains the current value. Since Ref is always guaranteed to have a value, the returned action completes immediately after being bound.abstract fun get(): Kind<F, A>
getAndSet Replaces the current value with a, returning the old value.abstract fun getAndSet(a: A): Kind<F, A>
getAndUpdate Modifies the current value using the supplied update function and returns the old value.abstract fun getAndUpdate(f: (A) -> A): Kind<F, A>
modify Like update but allows the update function to return an output value of type B.abstract fun <B> modify(f: (A) -> Tuple2<A, B>): Kind<F, B>
set Sets the current value to a. The returned action completes after the reference has been successfully set.abstract fun set(a: A): Kind<F, Unit>
setAndGet Replaces the current value with a, returning the new value.abstract fun setAndGet(a: A): Kind<F, A>
tryModify Like tryUpdate but allows the update function to return an output value of type B.abstract fun <B> tryModify(f: (A) -> Tuple2<A, B>): Kind<F, Option<B>>
tryUpdate Attempts to modify the current value once, in contrast to update which calls f until it succeeds.abstract fun tryUpdate(f: (A) -> A): Kind<F, Boolean>
update Updates the current value using the supplied function f.abstract fun update(f: (A) -> A): Kind<F, Unit>
updateAndGet Modifies the current value using the supplied update function and returns the new value.abstract fun updateAndGet(f: (A) -> A): Kind<F, A>

Companion Object Functions

factory Build a RefFactory value for creating Ref types F without deciding the type of the Ref’s value.fun <F> factory(MD: MonadDefer<F>): RefFactory<F>
invoke Creates an asynchronous, concurrent mutable reference initialized using the supplied function.operator fun <F, A> invoke(MD: MonadDefer<F>, a: A): Kind<F, Ref<F, A>>
unsafe Like invoke but returns the newly allocated ref directly instead of wrapping it in MonadDefer.invoke. This method is considered unsafe because it is not referentially transparent – it allocates mutable state.fun <F, A> unsafe(a: A, MD: MonadDefer<F>): Ref<F, A>

Do you like Arrow?

Arrow Org
<