Clojure宏评估字符串

时间:2011-03-21 10:11:00

标签: clojure

我正在尝试创建一个将输入的符号转换为字符串的clojure宏。但是,当我这样做时:

(defmacro convert-to-string [something]
  `(call-converted "~something")
  )

(macroexpand '(convert-to-string convert-this))

:我明白了:

(call-converted "~something")

:而不是:

(call-converted "~convert-this")

:有没有人告诉我如何做到这一点?

2 个答案:

答案 0 :(得分:4)

您可以考虑使用关键字(或带引号的符号)和函数,而不是宏:

(defn convert-to-string [x] (call-converted (name x)))
(convert-to-string :foo)
(convert-to-string 'foo)

如果你真的想要一个宏:

(defmacro convert-to-string [x] `(call-converted ~(name x)))
(macroexpand-1 '(convert-to-string foo))
=> (user/call-converted "foo")

答案 1 :(得分:0)

我绝不是宏的专家,但这会解决你的问题:

(defmacro to-str [expr] (str expr))