The core data structures in Pointless are immutable. References serve as mutable wrappers around these values, allowing programs to leverage mutability. Note that references must not reference themselves (they must not contain circular references) as this could cause memory leaks and infinite recursion.
An example pseudorandom number generator function that uses a reference to store the generator state.
seed = Ref.of(0)
-- Linear congruential generator parameters
m = 2 ** 32
a = 1664525
c = 1013904223
fn random()
next = (a * Ref.get(seed) + c) % m -- Calculate new seed
Ref.set(seed, next) -- Store new seed
next / m -- Return scaled value
end
random()
random()
random()
random()
panic: variable is not defined
name: Ref
seed = Ref.of(0)
^
At pages/stdlib/Ref/index.md:embedded:1:8
Module Contents
Ref.get(ref)
Get the value stored in ref.
Ref = import "std:Ref"
score = Ref.of(0)
Ref.get(score)
0
Ref.of(value)
Create a mutable reference containing value.
Ref = import "std:Ref"
Ref.of(0)
Ref.of(0)
Ref.put(value, ref)
Update ref to store value. Return
value.
Ref = import "std:Ref"
score = Ref.of(0)
Ref.put(1, score)
score
1
Ref.of(1)
Ref.set(ref, value)
Update ref to store value. Return
ref.
Ref = import "std:Ref"
score = Ref.of(0)
Ref.set(score, 1)
score
Ref.of(1)
Ref.of(1)