<B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (
self: Ref<A>
) => Effect.Effect<B>
<A, B>(
self: Ref<A>,
pf: (a: A) => readonly [B, Option.Option<A>]
): Effect.Effect<B>Computes a result atomically and optionally updates the value of the Ref.
When to use
Use to compute a return value while optionally updating a plain Ref.
Details
The callback receives the current value and returns [result, nextValue],
where nextValue is an Option. If nextValue is Option.some(value),
the Ref is updated to value; if it is Option.none(), the Ref is left
unchanged. The returned effect always succeeds with result.
Example (Conditionally modifying a value)
import { Effect, Option, Ref } from "effect"
const program = Effect.gen(function*() {
const counter = yield* Ref.make(5)
// Only modify if value is greater than 3
const result1 = yield* Ref.modifySome(
counter,
(n) =>
n > 3
? [`incremented ${n}`, Option.some(n + 10)]
: ["no change", Option.none()]
)
console.log(result1) // "incremented 5"
const current1 = yield* Ref.get(counter)
console.log(current1) // 15
// Try to modify with a condition that fails
const result2 = yield* Ref.modifySome(
counter,
(n) =>
n < 10
? [`decremented ${n}`, Option.some(n - 5)]
: ["no change", Option.none()]
)
console.log(result2) // "no change"
const current2 = yield* Ref.get(counter)
console.log(current2) // 15 (unchanged)
})export const const modifySome: {
<B, A>(
pf: (a: A) => readonly [B, Option.Option<A>]
): (self: Ref<A>) => Effect.Effect<B>
<A, B>(
self: Ref<A>,
pf: (a: A) => readonly [B, Option.Option<A>]
): Effect.Effect<B>
}
Computes a result atomically and optionally updates the value of the Ref.
When to use
Use to compute a return value while optionally updating a plain Ref.
Details
The callback receives the current value and returns [result, nextValue],
where nextValue is an Option. If nextValue is Option.some(value),
the Ref is updated to value; if it is Option.none(), the Ref is left
unchanged. The returned effect always succeeds with result.
Example (Conditionally modifying a value)
import { Effect, Option, Ref } from "effect"
const program = Effect.gen(function*() {
const counter = yield* Ref.make(5)
// Only modify if value is greater than 3
const result1 = yield* Ref.modifySome(
counter,
(n) =>
n > 3
? [`incremented ${n}`, Option.some(n + 10)]
: ["no change", Option.none()]
)
console.log(result1) // "incremented 5"
const current1 = yield* Ref.get(counter)
console.log(current1) // 15
// Try to modify with a condition that fails
const result2 = yield* Ref.modifySome(
counter,
(n) =>
n < 10
? [`decremented ${n}`, Option.some(n - 5)]
: ["no change", Option.none()]
)
console.log(result2) // "no change"
const current2 = yield* Ref.get(counter)
console.log(current2) // 15 (unchanged)
})
modifySome: {
<function (type parameter) B in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>B, function (type parameter) A in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>A>(pf: (a: A) => readonly [B, Option.Option<A>]pf: (a: Aa: function (type parameter) A in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>A) => readonly [function (type parameter) B in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>B, import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <B, A>(pf: (a: A) => readonly [B, Option.Option<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 <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) B in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>B>
<function (type parameter) A in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A, function (type parameter) B in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<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>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A>, pf: (a: A) => readonly [B, Option.Option<A>]pf: (a: Aa: function (type parameter) A in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A) => readonly [function (type parameter) B in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>B, import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A>]): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) B in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>B>
} = import dualdual<
<function (type parameter) B in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>B, function (type parameter) A in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>A>(
pf: (a: A) => readonly [B, Option.Option<A>]pf: (a: Aa: function (type parameter) A in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>A) => readonly [function (type parameter) B in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>B, import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <B, A>(pf: (a: A) => readonly [B, Option.Option<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 <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) B in <B, A>(pf: (a: A) => readonly [B, Option.Option<A>]): (self: Ref<A>) => Effect.Effect<B>B>,
<function (type parameter) A in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A, function (type parameter) B in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<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>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A>,
pf: (a: A) => readonly [B, Option.Option<A>]pf: (a: Aa: function (type parameter) A in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A) => readonly [function (type parameter) B in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>B, import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<A>]): Effect.Effect<B>A>]
) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) B in <A, B>(self: Ref<A>, pf: (a: A) => readonly [B, Option.Option<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, pf: (a: A) => readonly [B, Option.Option<A>]pf) =>
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(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, (value: anyvalue) => {
const [const b: anyb, const option: Option.Option<A>option] = pf: (a: A) => readonly [B, Option.Option<A>]pf(value: anyvalue)
return [const b: anyb, const option: Option.Option<A>option._tag === "None" ? value: anyvalue : const option: Option.Some<A>const option: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: 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; <…;
toString: () => string;
toJSON: () => unknown;
}
option.value]
}))