Atom


Overview

std.atom provides a single-cell mutable reference with compare-and-set (CAS) semantics.

Key points:

std.atom is not re-exported by std.prelude. Import it explicitly.


Quick Start

(module demo
  (import std.atom std.io)
  (begin
    (define counter (atom 0))
    (println (deref counter))                          ;; 0
    (println (swap! counter (lambda (x) (+ x 1))))    ;; 1
    (println (compare-and-set! counter 1 10))         ;; #t
    (println (deref counter))))                        ;; 10

API

Collision-safe exports:

FunctionSignatureDescription
atom:new(value) -> atomCreate a new atom with initial value.
atom:atom?(x) -> boolTrue iff x is an atom.
atom:deref(a) -> valueRead the current atom value.
atom:reset!(a value) -> valueStore value and return it.
atom:swap!(a fn arg ...) -> valueCompute and install a new value by applying fn to old value plus extra args.
atom:compare-and-set!(a old new) -> boolCAS from old to new; returns #t on success, #f otherwise.

Clojure-style aliases (same behavior):

atom, atom?, deref, reset!, swap!, compare-and-set!


Semantics


Import Notes

std.core already exports atom? with Scheme semantics (“not a pair”). When both modules are needed, prefer the collision-safe names from std.atom or use filtered imports:

(import std.core)
(import (only std.atom atom:new atom:deref atom:swap! atom:compare-and-set!))