Ref

public class Ref<F, A>

Ref is an asynchronous, concurrent mutable reference in the context of F holding a value of type A. Provides safe concurrent access and modification of its content. Refis a purely functional wrapper over an Atomic<A> in context F that is always initialized to a value.

  • Obtains the current value. Since Ref is always guaranteed to have a value, the returned action completes immetiately after being bound.

    Declaration

    Swift

    public func get() -> Kind<F, A>

    Return Value

    A computation that provides the underlying value.

  • Sets the current value to a new value. The returned action completes after the reference has been successfully set.

    Declaration

    Swift

    public func set(_ a: A) -> Kind<F, ()>

    Parameters

    a

    New value to be set.

    Return Value

    A computation that sets the underlying value.

  • Replaces the current value, returning the old value.

    Declaration

    Swift

    public func getAndSet(_ a: A) -> Kind<F, A>

    Parameters

    a

    New value to be set.

    Return Value

    A computation that provides the old value saved in this reference.

  • Replaces the current value, returning the new value.

    Declaration

    Swift

    public func setAndGet(_ a: A) -> Kind<F, A>

    Parameters

    a

    New value to be set.

    Return Value

    A computation that provides the new value saved in this reference.

  • Updates the current value with a function. If another modification occurs between the time the current value is read and subsequently updated, the modification is retried using the new value. Hence, the function may be invoked multiple times.

    Declaration

    Swift

    public func update(_ f: @escaping (A) -> A) -> Kind<F, ()>

    Parameters

    f

    Transforming function.

    Return Value

    A computation that updates the underlying value using the provided function.

  • Modifies the current value using the supplied update function and returns the old value.

    Declaration

    Swift

    public func getAndUpdate(_ f: @escaping (A) -> A) -> Kind<F, A>

    Parameters

    f

    Transforming function.

    Return Value

    A computation that provides the old value saved in this reference.

  • Modifies the current value using the supplied update function and returns the new value.

    Declaration

    Swift

    public func updateAndGet(_ f: @escaping (A) -> A) -> Kind<F, A>

    Parameters

    f

    Transforming function.

    Return Value

    A computation that provides the new value saved in this reference.

  • Updates the reference but allows the function to return an output of a different type.

    Declaration

    Swift

    public func modify<B>(_ f: @escaping (A) -> (A, B)) -> Kind<F, B>

    Parameters

    f

    Transforming function.

    Return Value

    A computation that updates the underlying value using the provided function.

  • Tries to modify the current value once, in contrast to update which calls the function until it succeeds.

    Declaration

    Swift

    public func tryUpdate(_ f: @escaping (A) -> A) -> Kind<F, Bool>

    Parameters

    f

    Transforming function.

    Return Value

    A computation that contains a flag indicating if the update was possible or not.

  • Tries to modify the current value once, in contrast to modify which calls the function until it succeeds.

    Declaration

    Swift

    public func tryModify<B>(_ f: @escaping (A) -> (A, B)) -> Kind<F, Option<B>>

    Parameters

    f

    Transforming function.

    Return Value

    A computation that provides an optional value with the result of the modification, if it happened.

  • Obtains a snapshot of the current value and a setter for updating it. The setter will return false if another concurrent call invalidated the snapshot; i.e. modified the value. It will return true if setting the value was successful. Once it has returned false or been used once, a setter never succeeds again.

    Declaration

    Swift

    public func access() -> Kind<F, (A, (A) -> Kind<F, Bool>)>

    Return Value

    A computation providing a value and a setter.

  • Safely creates a Ref.

    Declaration

    Swift

    static func of(_ f: @autoclosure @escaping () -> A) -> Kind<F, Ref<F, A>>

    Parameters

    f

    Function providing the initial value for the reference.

    Return Value

    A computation that provides the Ref.

  • Unsafely creates a Ref.

    Declaration

    Swift

    static func unsafe(_ a: A) -> Ref<F, A>

    Parameters

    a

    Initial value for the reference.

    Return Value

    A Ref containing the initial value.