Ref<A>A mutable reference that provides atomic read, write, and update operations.
When to use
Use to keep shared mutable state that is read and updated inside Effect programs.
Details
A Ref is a thread-safe mutable reference type for shared state. It supports
simple read and write operations as well as atomic transformations.
Example (Reading and updating a ref)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
// Create a ref with initial value
const counter = yield* Ref.make(0)
// Read the current value
const value = yield* Ref.get(counter)
console.log(value) // 0
// Update the value atomically
yield* Ref.update(counter, (n) => n + 1)
// Read the updated value
const newValue = yield* Ref.get(counter)
console.log(newValue) // 1
})export interface interface Ref<in out A>A mutable reference that provides atomic read, write, and update operations.
When to use
Use to keep shared mutable state that is read and updated inside Effect
programs.
Details
A Ref is a thread-safe mutable reference type for shared state. It supports
simple read and write operations as well as atomic transformations.
Example (Reading and updating a ref)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
// Create a ref with initial value
const counter = yield* Ref.make(0)
// Read the current value
const value = yield* Ref.get(counter)
console.log(value) // 0
// Update the value atomically
yield* Ref.update(counter, (n) => n + 1)
// Read the updated value
const newValue = yield* Ref.get(counter)
console.log(newValue) // 1
})
The Ref namespace containing type definitions and utilities.
When to use
Use when referring to type members nested under the Ref namespace.
Ref<in out function (type parameter) A in Ref<in out A>A> extends Ref.interface Ref<in out A>.Variance<in out A>Variance interface for Ref types, defining the type parameter constraints.
When to use
Use when working with the type-level variance marker carried by Ref.
Example (Using invariant refs)
import { Effect, Ref } from "effect"
// This interface defines the invariant nature of Ref's type parameter
// A Ref<A> is both a producer and consumer of A
const program = Effect.gen(function*() {
const ref = yield* Ref.make(42)
// Ref is invariant - it can both produce and consume numbers
const value = yield* Ref.get(ref) // produces number
yield* Ref.set(ref, value + 1) // consumes number
})
Variance<function (type parameter) A in Ref<in out A>A>, import PipeablePipeable {
readonly Ref<in out A>.ref: MutableRef.MutableRef<A>(property) Ref<in out A>.ref: {
current: T;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
ref: import MutableRefMutableRef.type MutableRef.MutableRef = /*unresolved*/ anyMutableRef<function (type parameter) A in Ref<in out A>A>
}
/**
* The Ref namespace containing type definitions and utilities.
*
* **When to use**
*
* Use when referring to type members nested under the `Ref` namespace.
*
* @since 2.0.0
*/
export declare namespace Ref {
/**
* Variance interface for Ref types, defining the type parameter constraints.
*
* **When to use**
*
* Use when working with the type-level variance marker carried by `Ref`.
*
* **Example** (Using invariant refs)
*
* ```ts
* import { Effect, Ref } from "effect"
*
* // This interface defines the invariant nature of Ref's type parameter
* // A Ref<A> is both a producer and consumer of A
* const program = Effect.gen(function*() {
* const ref = yield* Ref.make(42)
*
* // Ref is invariant - it can both produce and consume numbers
* const value = yield* Ref.get(ref) // produces number
* yield* Ref.set(ref, value + 1) // consumes number
* })
* ```
*
* @category models
* @since 2.0.0
*/
export interface interface Ref<in out A>.Variance<in out A>Variance interface for Ref types, defining the type parameter constraints.
When to use
Use when working with the type-level variance marker carried by Ref.
Example (Using invariant refs)
import { Effect, Ref } from "effect"
// This interface defines the invariant nature of Ref's type parameter
// A Ref<A> is both a producer and consumer of A
const program = Effect.gen(function*() {
const ref = yield* Ref.make(42)
// Ref is invariant - it can both produce and consume numbers
const value = yield* Ref.get(ref) // produces number
yield* Ref.set(ref, value + 1) // consumes number
})
Variance<in out function (type parameter) A in Variance<in out A>A> {
readonly [const TypeId: "~effect/Ref"TypeId]: {
readonly _A: Invariant<A>_A: type Invariant<A> = (_: A) => AFunction-type alias encoding invariant variance for a phantom type
parameter.
When to use
Use as a phantom field type to make a type parameter invariant, neither
covariant nor contravariant.
Details
A value of type Invariant<A> cannot be assigned to Invariant<B> unless
A and B are the same type.
Example (Defining an invariant phantom type)
import type { Types } from "effect"
interface Container<T> {
readonly _phantom: Types.Invariant<T>
readonly value: T
}
Namespace for
Invariant
-related utilities.
When to use
Use when referring to type-level helpers nested under Invariant.
Invariant<function (type parameter) A in Variance<in out A>A>
}
}
}