noziar
noziar

Reputation: 1057

Accessing argument's metadata in Clojure macro

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, ...}

Upvotes: 3

Views: 201

Answers (1)

noziar
noziar

Reputation: 1057

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.

Upvotes: 5

Related Questions