【发布时间】:2016-06-27 07:43:35
【问题描述】:
我似乎对 Clojure 中的宏很感兴趣。我可能缺少一些基本的东西。首先,请允许我描述一个我想要的示例。
(defmacro macrotest [v] `(d ...))
(def a '(b c))
(macroexpand-1 '(macrotest a))
; => (d (b c))
换句话说,传递给macrotest 的var 已解析,但未进一步评估。
为宏提供 var 的值有效:
(defmacro macrotest [v] `(d ~v))
(macroexpand-1 '(macrotest (b c)))
; => (d (b c))
但提供 var 不会:
(def a '(b c))
(macroexpand-1 '(macrotest a))
; => (d a)
是否可以在 Clojure 宏中解析 var,但不评估其值?
编辑:eval 似乎可以实现我想要的:
(defmacro macrotest [v] `(d ~(eval v)))
(def a '(b c))
(macroexpand-1 '(macrotest a))
; => (user/d (b c))
【问题讨论】: