Values and mutation
A record is a heap object. Assigning it another name shares that object; all fields are mutable. The field-path example shows nominal selection through nested records.
Select an enum case with match. Put mutable payload in a record, then use
● and set! to read or change its fields:
(import
(snail core base)
(snail core io))
(record Radius
((value I64)))
(enum Shape (Circle ((radius Radius))) (Empty))
(record LinkData
((value I64)
(next (Option Chain))))
(enum Chain (Link ((data LinkData))) (End))
(define (cycle) (→ Chain)
(let* ((data (new LinkData (value 3) (next (None)))) (link (Link data)))
(set! (● data next) (Some link))
link))
(define (main (out OutputPort)) (→ Unit)
(let* ((circle (Circle (new Radius (value 4)))) (alias circle))
(match alias
((Circle radius) (set! (● radius value) 9))
((Empty) (panic "empty shape")))
(match circle
((Circle radius)
(write-line out (show (● radius value)))
(write-line out (show (● radius value)))
(set! (● radius value) 12))
((Empty) (panic "empty shape")))
(match alias
((Circle radius) (write-line out (show (● radius value))))
((Empty) (panic "empty shape"))))
(match (cycle)
((Link first)
(set! (● first value) 21)
(match (● first next)
((Some (Link same)) (write-line out (show (● same value))))
(_ (panic "missing cycle")))
(match (cycle)
((Link second) (write-line out (show (● second value))))
((End) (panic "empty chain"))))
((End) (panic "empty chain")))
(unit))
9
9
12
21
3
The first three lines show two names observing one object. The last two show a cyclic object’s internal alias and a separate call to its factory. Each call constructs a fresh graph; the second graph keeps its original value. Bind a factory result once and pass it to callers that should share state.
Any value can be shared this way, with no borrow syntax or explicit cloning; the collector reclaims it once nothing reaches it. See values for sharing, closures and resources.