FuncType - v0.60.3
    Preparing search index...

    Interface RefType<A>

    A mutable reference container that holds a value of type A. This provides controlled mutability in a functional context.

    Variance: invariant in A by design. Ref is a mutable cell — set(A) writes A (contravariant) and get(): A reads A (covariant), so A is genuinely invariant. Unlike the other containers in functype, Ref has no <out A> annotation and cannot be widened via subtyping. This mirrors Scala's scala.concurrent.stm.Ref and reflects the fundamental nature of mutable state: you can't safely treat a Ref[Narrow] as a Ref[Wide] because someone could set a Wide value that isn't a Narrow.

    const counter = Ref(0)
    counter.get() // 0
    counter.set(5)
    counter.get() // 5
    counter.update(n => n + 1)
    counter.get() // 6
    interface RefType<A> {
        compareAndSet(expected: A, newValue: A): boolean;
        get(): A;
        getAndSet(value: A): A;
        getAndUpdate(f: (current: A) => A): A;
        modify<B>(f: (current: A) => [A, B]): B;
        set(value: A): void;
        update(f: (current: A) => A): void;
        updateAndGet(f: (current: A) => A): A;
    }

    Type Parameters

    • A
    Index

    Methods

    • Compare and swap - only updates if current value equals expected

      Parameters

      • expected: A
      • newValue: A

      Returns boolean

    • Get the current value

      Returns A

    • Update and return the old value

      Parameters

      • value: A

      Returns A

    • Update and return the old value

      Parameters

      • f: (current: A) => A

      Returns A

    • Modify the value and return a result

      Type Parameters

      • B

      Parameters

      • f: (current: A) => [A, B]

      Returns B

    • Set a new value

      Parameters

      • value: A

      Returns void

    • Update the value using a function

      Parameters

      • f: (current: A) => A

      Returns void

    • Update and return the new value

      Parameters

      • f: (current: A) => A

      Returns A