Clojure. Cursive. REPL. Windows 10. All up-to-date.
Here's my 'base' macro:
(defmacro PutProp! [Sym Val PName]
`(reset-meta! (var ~Sym) (assoc (meta (var ~Sym)) ~PName ~Val)))
I have no idea if this is even correct in doing what I want to do, which is to modify Sym's metadata keyname PName with the new Val value. I'm new at this and I'm SURE I've jumped into the pool at the deep end. I'm using a macro because I thought it would avoid messy variable binding issues which I don't understand in Clojure.
Then I wrote another macro to define a lot of metadata on a single call:
(defmacro DefMeta! [L] ;; Given a symbol (first L), define metadata for it given in (next L).
`(let [S (first ~@L)] ;; The symbol we're modifying is the first element in list L.
(map (fn [[K V]] (PutProp! S V K)) (next ~@L)))) ;; (next L) is the list of key/value pairs.
I'm also not sure this is what I want to write. Working at the REPL, I define a symbol:
(def Sym 0)
Then I do a macroexpand-1 of DefMeta!:
(macroexpand-1 '(DefMeta! [Sym :VName :VValue]))
I get the following:
(clojure.core/let
[thic.core/S (clojure.core/first Sym :VName :VValue)]
(clojure.core/map
(clojure.core/fn [[thic.core/K thic.core/V]] (thic.core/PutProp! thic.core/S thic.core/V thic.core/K))
(clojure.core/next Sym :VName :VValue)))
which doesn't macro-expand the PutProp! macro.
I'm stumped. I have four books on Clojure programming and none of them mentions macro calls from within macro calls. Is it even legal? If so, how do I fully expand the inner macro so that I can see if what I've written is what I want? [...the perennial programmer problem...]
PS I tried macroexpand-all and I'm overwhelmed with the macroexpansion of EVERYTHING, even Clojure core functions.
Something less than that, please. Please?
PutProp!does not need to be a macro, and could be written as a simple function (much preferred). - Alan Thompsondef-meta!for names. CamelCase names likeDefMeta!are normally reserved for interaction with Java classes or their Clojure cousins, records. - Alan Thompsonmacroexpand-1on the result of the firstmacroexpand-1if you're just wanting one more level of expansion? - Travis Sunderland