<A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>
<A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>Modifies the value of the Ref atomically using the given function.
When to use
Use to compute both a separate return value and the next stored Ref value
in one atomic update.
Details
The function receives the current value and returns a tuple of
[result, newValue]. The Ref is updated with newValue, and result is
returned by the effect.
Example (Modifying a value atomically)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
const counter = yield* Ref.make(10)
// Modify the ref and return some computation result
const result = yield* Ref.modify(counter, (n) => [
`Previous value was ${n}`, // Return value
n * 2 // New ref value
])
console.log(result) // "Previous value was 10"
const current = yield* Ref.get(counter)
console.log(current) // 20
})
// Example with more complex computation
const program2 = Effect.gen(function*() {
const state = yield* Ref.make({ count: 0, total: 0 })
const incremented = yield* Ref.modify(state, (s) => [
s.count, // Return previous count
{ count: s.count + 1, total: s.total + s.count + 1 } // New state
])
console.log(incremented) // 0
})export const const modify: (<A, B>(
f: (a: A) => readonly [B, A]
) => (self: Ref<A>) => Effect.Effect<B>) &
(<A, B>(
self: Ref<A>,
f: (a: A) => readonly [B, A]
) => Effect.Effect<B>)
Modifies the value of the Ref atomically using the given function.
When to use
Use to compute both a separate return value and the next stored Ref value
in one atomic update.
Details
The function receives the current value and returns a tuple of
[result, newValue]. The Ref is updated with newValue, and result is
returned by the effect.
Example (Modifying a value atomically)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
const counter = yield* Ref.make(10)
// Modify the ref and return some computation result
const result = yield* Ref.modify(counter, (n) => [
`Previous value was ${n}`, // Return value
n * 2 // New ref value
])
console.log(result) // "Previous value was 10"
const current = yield* Ref.get(counter)
console.log(current) // 20
})
// Example with more complex computation
const program2 = Effect.gen(function*() {
const state = yield* Ref.make({ count: 0, total: 0 })
const incremented = yield* Ref.modify(state, (s) => [
s.count, // Return previous count
{ count: s.count + 1, total: s.total + s.count + 1 } // New state
])
console.log(incremented) // 0
})
modify = import dualdual<
<function (type parameter) A in <A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>A, function (type parameter) B in <A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>B>(f: (a: A) => readonly [B, A]f: (a: Aa: function (type parameter) A in <A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>A) => readonly [function (type parameter) B in <A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>B, function (type parameter) A in <A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>A]) => (self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<A>;
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; <…;
}
self: 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<function (type parameter) A in <A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) B in <A, B>(f: (a: A) => readonly [B, A]): (self: Ref<A>) => Effect.Effect<B>B>,
<function (type parameter) A in <A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>A, function (type parameter) B in <A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>B>(self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<A>;
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; <…;
}
self: 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<function (type parameter) A in <A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>A>, f: (a: A) => readonly [B, A]f: (a: Aa: function (type parameter) A in <A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>A) => readonly [function (type parameter) B in <A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>B, function (type parameter) A in <A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>A]) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) B in <A, B>(self: Ref<A>, f: (a: A) => readonly [B, A]): Effect.Effect<B>B>
>(2, (self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<A>;
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; <…;
}
self, f: anyf) =>
import EffectEffect.sync(() => {
const [const b: anyb, const a: anya] = f: anyf(self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<A>;
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; <…;
}
self.ref.current)
self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<A>;
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; <…;
}
self.ref.current = const a: anya
return const b: anyb
}))