(self: MutableRef<number>): numberIncrements a numeric MutableRef by 1 and returns the previous value.
When to use
Use to read the current numeric MutableRef value before incrementing it.
Example (Reading before incrementing)
import { MutableRef } from "effect"
const counter = MutableRef.make(5)
// Get current value and then increment
const previousValue = MutableRef.getAndIncrement(counter)
console.log(previousValue) // 5
console.log(MutableRef.get(counter)) // 6
// Useful for ID generation
const idGenerator = MutableRef.make(0)
const getId = () => MutableRef.getAndIncrement(idGenerator)
console.log(getId()) // 0
console.log(getId()) // 1
console.log(getId()) // 2
// Post-increment semantics (like i++ in other languages)
const position = MutableRef.make(0)
const currentPos = MutableRef.getAndIncrement(position)
console.log(`Was at: ${currentPos}, Now at: ${MutableRef.get(position)}`) // "Was at: 0, Now at: 1"
// Useful for iteration counters
const iterations = MutableRef.make(0)
while (MutableRef.get(iterations) < 5) {
const iteration = MutableRef.getAndIncrement(iterations)
console.log(`Iteration ${iteration}`)
}export const const getAndIncrement: (
self: MutableRef<number>
) => number
Increments a numeric MutableRef by 1 and returns the previous value.
When to use
Use to read the current numeric MutableRef value before incrementing it.
Example (Reading before incrementing)
import { MutableRef } from "effect"
const counter = MutableRef.make(5)
// Get current value and then increment
const previousValue = MutableRef.getAndIncrement(counter)
console.log(previousValue) // 5
console.log(MutableRef.get(counter)) // 6
// Useful for ID generation
const idGenerator = MutableRef.make(0)
const getId = () => MutableRef.getAndIncrement(idGenerator)
console.log(getId()) // 0
console.log(getId()) // 1
console.log(getId()) // 2
// Post-increment semantics (like i++ in other languages)
const position = MutableRef.make(0)
const currentPos = MutableRef.getAndIncrement(position)
console.log(`Was at: ${currentPos}, Now at: ${MutableRef.get(position)}`) // "Was at: 0, Now at: 1"
// Useful for iteration counters
const iterations = MutableRef.make(0)
while (MutableRef.get(iterations) < 5) {
const iteration = MutableRef.getAndIncrement(iterations)
console.log(`Iteration ${iteration}`)
}
getAndIncrement = (self: MutableRef<number>(parameter) self: {
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;
}
self: 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<number>): number => const getAndUpdate: {
<T>(f: (value: T) => T): (
self: MutableRef<T>
) => T
<T>(self: MutableRef<T>, f: (value: T) => T): T
}
getAndUpdate(self: MutableRef<number>(parameter) self: {
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;
}
self, (n: numbern) => n: numbern + 1)