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>, Pipeable {
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.interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<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>
}
}
}