antimonia/mutable

The mutable module allows you to use mutable values of any type within your code.

Types

The Mutable generic type houses a reference to the value of the specified type stored elsewhere in memory.

Implementation notes:

  • On JavaScript, Mutable(a) values correspond to an object of type { value: a }. This may be subject to change, so if you (for some reason) are writing bindings in JavaScript that requires the Mutable type, please don’t use this.

  • On Erlang, mutability is achieved through a process that is sent get or set operations, and loops with a new value.

pub type Mutable(a)

Values

pub fn from(value: a) -> Mutable(a)

Creates a Mutable from a value.

pub fn get(mut: Mutable(a)) -> a

Gets the value of a Mutable.

pub fn set(mut: Mutable(a), value: a) -> Nil

Updates the value of a Mutable.

pub fn to_tuple(mut: Mutable(a)) -> #(fn() -> a, fn(a) -> Nil)

Converts a Mutable to a #(getter, setter) tuple.

Example

fn update_mutable_handler(mut: Mutable(Int)) -> Nil {
  let #(val, set_val) = to_tuple(mut)
  case val() {
    0 -> set_val(2)
    _ -> set_val(0)
  }
}
pub fn tuple_from(value: a) -> #(fn() -> a, fn(a) -> Nil)

Creates a #(getter, setter) tuple from a value.

Example

let #(counter, set_counter) = tuple_from(3)

counter()
// -> 3
set_counter(counter() + 2)
// -> Nil
counter()
// -> 5

Search Document