<A>(value: A): (self: Ref<A>) => Effect.Effect<A>
<A>(self: Ref<A>, value: A): Effect.Effect<A>Sets the value of the Ref atomically to the specified value and returns the new value.
When to use
Use when you want to set a Ref value and immediately get it back in one
atomic operation.
Example (Setting and returning the new value)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
const ref = yield* Ref.make(10)
// Set new value and get it back in one operation
const newValue = yield* Ref.setAndGet(ref, 42)
console.log(newValue) // 42
// Verify the ref contains the new value
const current = yield* Ref.get(ref)
console.log(current) // 42
})
// Useful for sequential operations
const program2 = Effect.gen(function*() {
const counter = yield* Ref.make(0)
const newValue = yield* Ref.setAndGet(counter, 20)
console.log(newValue) // 20
})export const const setAndGet: (<A>(
value: A
) => (self: Ref<A>) => Effect.Effect<A>) &
(<A>(
self: Ref<A>,
value: A
) => Effect.Effect<A>)
Sets the value of the Ref atomically to the specified value and returns the new value.
When to use
Use when you want to set a Ref value and immediately get it back in one
atomic operation.
Example (Setting and returning the new value)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
const ref = yield* Ref.make(10)
// Set new value and get it back in one operation
const newValue = yield* Ref.setAndGet(ref, 42)
console.log(newValue) // 42
// Verify the ref contains the new value
const current = yield* Ref.get(ref)
console.log(current) // 42
})
// Useful for sequential operations
const program2 = Effect.gen(function*() {
const counter = yield* Ref.make(0)
const newValue = yield* Ref.setAndGet(counter, 20)
console.log(newValue) // 20
})
setAndGet = import dualdual<
<function (type parameter) A in <A>(value: A): (self: Ref<A>) => Effect.Effect<A>A>(value: Avalue: function (type parameter) A in <A>(value: A): (self: Ref<A>) => Effect.Effect<A>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>(value: A): (self: Ref<A>) => Effect.Effect<A>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A>(value: A): (self: Ref<A>) => Effect.Effect<A>A>,
<function (type parameter) A in <A>(self: Ref<A>, value: A): Effect.Effect<A>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>(self: Ref<A>, value: A): Effect.Effect<A>A>, value: Avalue: function (type parameter) A in <A>(self: Ref<A>, value: A): Effect.Effect<A>A) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A>(self: Ref<A>, value: A): Effect.Effect<A>A>
>(2, <function (type parameter) A in <A>(self: Ref<A>, value: A): anyA>(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>(self: Ref<A>, value: A): anyA>, value: Avalue: function (type parameter) A in <A>(self: Ref<A>, value: A): anyA) => import EffectEffect.sync(() => 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<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.current = value: Avalue))