flan/conditions-play.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

52 lines
1.6 KiB
Plaintext

;;;; A program to poke at conditions and restarts with — see conditions.org.
;;;;
;;;; It loops instead of exiting, so `flan dev conditions-play.flan` can attach
;;;; and you can redefine `probe` and `run-once` from Emacs while it runs. The
;;;; socket written here is only a fallback; the daemon overrides it.
(import agent "vendor:agent")
(defstruct AssetMissing [id i32])
(defstruct Corrupt [id i32])
;;; Handlers cannot see the establishing function's locals yet, so anything a
;;; handler accumulates into has to be a global.
(defonce seen i64)
(defonce ticks i64)
;;; Two frames under the restart-case, so a transfer has something to cross.
(defn probe [n i32] i32
(signal (AssetMissing {.id n}))
100)
(defn middle [n i32] i32
(defer (set ticks (+ ticks 1))) ; runs on the way out, transfer or not
(+ (probe n) 1))
;;; The shape spec-conditions.md §1 uses: a restart-case in value position,
;;; whose fall-through has to produce the type too.
(defn fetch [n i32] i32
(restart-case (middle n)
(use-placeholder [] -1)
(retry [] 7)))
(defn run-once [] ()
(print (fetch 1)) (println "")
(handler-bind [(AssetMissing [c] (set seen (+ seen (i64 (.id c)))))]
(print (fetch 2)) (println ""))
(handler-bind [(AssetMissing [c] (invoke-restart 'use-placeholder))]
(print (fetch 3)) (println ""))
(print seen) (println "")
(print ticks) (println ""))
(defn main [] i32
(agent/start "/tmp/flan-conditions.sock")
(run-once)
(while (= (agent/wait 200) 0) 0)
(run-once)
(while (= (agent/wait 200) 0) 0)
(run-once)
0)