Comming from Common Lisp, I'm trying to use let to shadow the value of a global variable dynamically.
(setv glob 18)
(defn callee []
(print glob))
(defn nonl [x]
(callee)
(let [glob x]
(callee))
(callee))
(nonl 39)
=>
18
18
18
Is there a way to make this work so that the second call to callee gives 39?
[EDIT]
Based on gilch's response i wrote the following draft using contextvars:
(import contextvars :as cv)
(defmacro defparam [symbol value]
(let [command (. f"{symbol} = cv.ContextVar('{symbol}')")]
`(do (exec ~command)
(.set ~symbol ~value))))
(defmacro parameterize [symbol value #* body]
`(. (cv.copy-context)
(run (fn [] (do (.set ~symbol ~value)
~@body)))))
(defn callee []
(glob.get))
;;;;;;;;;
(defparam glob 18)
(callee) => 18
(parameterize glob 39
(callee)) => 39
(callee) => 18
Thanks for the answers!