interior/cell

A Cell is a mutable container.

It can be used to provide interior mutability, allowing for mutating state in an otherwise immutable object.

⚠️ Disclaimer

Types in Gleam are immutable for a reason! As such, you should use immutable data structures whenever possible.

Don’t use this library to shortcut structuring your code and APIs in a functional way that embraces immutability!

That being said, there are some scenarios where it can be handy to have shared mutable state, such as when implementing mock objects for testing.

Target implementation

On the Erlang target, each Cell is backed by an actor.

On the JavaScript target, each Cell is a mutable memory location.

Types

A cell is a mutable container that holds a single value of type a.

pub opaque type Cell(a)

Values

pub fn get(cell: Cell(a)) -> a

Returns the contained value of the cell.

pub fn new(value: a) -> Cell(a)

Returns a new cell containing the given value.

pub fn set(cell: Cell(a), value: a) -> Nil

Sets the contained value of the cell to the given value.

pub fn update(cell: Cell(a), f: fn(a) -> a) -> Nil

Updates the contained value of the cell using the given function.

Search Document