Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing argument's metadata in Clojure macro

Tags:

macros

clojure

Is there a way to retrieve the metadata of the arguments inside a clojure macro without using eval? The only thing I could come up with so far is this:

(def ^{:a :b} my-var)

(defmacro my-macro [s] (prn (eval `(meta (var ~s)))))

(my-macro my-var)
;; Prints {:a :b, :name my-var, ...}
like image 356
noziar Avatar asked May 19 '26 16:05

noziar


1 Answers

I ended up finding a solution:

(def ^{:a :b} my-var)

(defmacro my-macro [s] (prn (meta (resolve s))))

(my-macro my-var)
;; Prints {:a :b, :name my-var, ...}

So the key part here is to use resolve function to get the var associated to the symbol.

like image 103
noziar Avatar answered May 21 '26 07:05

noziar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!