;; Example use of the persist macro

(macro persist [handle literal]
  ;; you can override the persisting if there is a params
  ;; module available at compile time
  (local (ok params?) (pcall (fn [] (require :params))))
  (local params (if ok params? {:persist true}))
  (if params.persist
    `(local ,handle
          (let [module# (or ... :entry-module)]
            (print (string.format "Persiting %s %s" module# ,(tostring handle)))
            (when (not _G._persist) (tset _G :_persist {}))
            (when (not (. _G._persist module#)) (tset _G :_persist module# {}))
            (when (not (. _G._persist module# ,(tostring handle)))
              (tset _G :_persist module# ,(tostring handle) ,literal))
            (. _G :_persist module# ,(tostring handle))))
    `(local ,handle ,literal)))

(persist something-i-want-to-keep {:value 1})

(local somthing-that-should-reset {:value 2})

(fn example []
  (print something-i-want-to-keep.a)
  (print something-that-should-reset.a))

{: example}

Generated by alexjgriffith using scpaste at Thu Nov 6 12:03:42 2025. EST. (original)