flan/test/programs/array-ctor.flan
Joseph Ferano a4c6b996ff def re-runs its initialiser, and defvar is renamed defonce
The trio the author decided on 2026-09-20 is now all built: def is CL's
defparameter — its initialiser runs on every daemon re-run, unguarded, so
an edited initialiser repaints the same storage on C-c C-c plus re-run —
defonce (Clojure's name for CL's defvar, per the author) initialises once
behind the .init~once. flag, and defconst stays the image.

One parse arm reads both forms; the difference is Ast.reinit, carried to
Tast.global's grerun. Emit.startup_plan gives a def no guard flag, and
Check.check_global lifts every def initialiser — zero and literal
included — into global/<n>, so the host's startup reaches it through the
function cell and a re-evaluated def swaps it (Session's def_inits;
Emit.redefinition declares the cell for a non-sibling target). The old
defvar spelling is refused with the rename and both compiling spellings,
and every program, test, doc and editor list is swept — except sand.flan,
the author's live WIP, whose seven defvar lines are flagged in FIX.org
and keep its three dependent tests red on this branch.
2026-09-21 07:12:04 +07:00

44 lines
1.5 KiB
Plaintext

;;;; (array COUNT TYPE) — the zeroed fixed array a [let] binding could not ask
;;;; for. A let has no type slot, so [4 V2] there is an array *literal* of two
;;;; elements and the second of them is a type name, which is an unknown name
;;;; and not a helpful error. The type spelling is untouched: `points` below is
;;;; still declared [3 V2], and the two are the same type, which is the point —
;;;; the constructor is assignable to the declaration.
(defstruct V2 [x f32 y f32])
(defconst n 3)
(defonce points [3 V2])
(defn sumx [ps [3 V2]] i32
(let [t (f32 0.0)]
(dotimes [i 3]
(set t (+ t (.x (at ps i)))))
(i32 t)))
(defn main [] i32
;; The case that cost 32 hand-written Vector2s: a local array of structs.
(let [pts (array 3 V2)]
(set (.x (at pts 0)) 1.5)
(set (.x (at pts 2)) 2.5)
(print (sumx pts)) (println "")) ; 4
;; Zeroed, not uninitialised: every element reads as the all-zero value.
(let [z (array 4 i32)]
(print (at z 3)) (println "")) ; 0
;; The count takes a constant's name, exactly as [n V2] does.
(let [c (array n V2)]
(set (.y (at c 1)) 7.0)
(print (i32 (.y (at c 1)))) (println "")) ; 7
;; Element types nest, and the result is assignable to a declaration written
;; the other way round — same type, two spellings.
(let [g (array 2 [2 i32])]
(set (at (at g 1) 1) 9)
(print (at (at g 1) 1)) (println "")) ; 9
(set points (array 3 V2))
(set (.x (at points 0)) 4.0)
(print (sumx points)) (println "") ; 4
0)