Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve symbol in macro

Tags:

clojure

I am stuck on a seemingly basic thing. I have a namespace where I have some definitions:

(ns my-namespace)

(def my-definition "HELLO")
(def my-definition2 "HI")

Now, I want to use the value of the vars in my-namespace in a macro, but I want to retrieve the symbols dynamically. E.g.,

(defmacro my-macro [n]
  (-> "my-namespace/my-definition" symbol resolve var-get))

Retrieving a symbol in such a manner works in a function (as long as the namespace is loaded), but not in a macro.

In a macro, the symbol cannot be resolved. I've tried quoting and unquoting but it still does not work.

Is it possible in a macro to use the value of a symbol created like that? If so, how?

like image 955
Erwin Rooijakkers Avatar asked Nov 18 '16 15:11

Erwin Rooijakkers


1 Answers

Try this one:

(defmacro my-macro
  [str]
  (-> str symbol resolve deref))
like image 142
Ertuğrul Çetin Avatar answered Sep 23 '22 00:09

Ertuğrul Çetin