I cannot seem to replace a dynamic var with binding
on a macrodefinition.
My goal is to
define a function with a macro which upon definition "fixes" a dynamic var.
(def ^:dynamic *pred?*
(fn []
(println "original pred")
false))
(defmacro macro-pred [fname a b]
`(defn ~fname []
(if (~*pred?*)
~a
~b))
)
(macro-pred demo 1 2)
(demo)
;; => 2
(defn my-pred []
(fn []
(println "my pred")
true))
(binding [*pred?* my-pred]
(macro-pred demo2 1 2))
(demo2);; => 2
(binding [*pred?* my-pred]
(macroexpand '(macro-pred demo2 1 2)))
;; => (def
;; demo2
;; (clojure.core/fn
;; ([] (if (#function[macros.demo/my-pred]) 1 2))))
In the provided snippet, I would expect the result of the (demo2)
call to be 1
. The macroexpand
seems to produce what I need, so.. what am I missing?