我认为auto-gensym(使用#
语法)在递归使用时可能会产生自我阴影,这看起来像一个bug。jumar确认在1.10.3、1.6.0、1.3.0、1.0.0中也会发生这种情况
(defmacro recursive-macro-1 [[x & more :as xs] acc]
(if (seq xs)
`(let [x# ~x]
(recursive-macro-1 ~more (conj ~acc x#)))
acc))
(defmacro recursive-macro-2 [[x & more :as xs] acc]
(if (seq xs)
(let [gx (gensym 'x)]
`(let [~gx ~x]
(recursive-macro-2 ~more (conj ~acc ~gx))))
acc))
(comment
(recursive-macro-1 [1 2] []) ;; => [2 2]
(recursive-macro-2 [1 2] []) ;; => [1 2]
)
我预计recursive-macro-1
将产生与recursive-macro-2
相同的结果,但从运行clojure.walk/macroexpand-all这些表单可以看出,阴影阻止了这种情况
;; auto-gensym - the name of the local binding is the same in the nested let*
(let* [x__10885__auto__ 1]
(let* [x__10885__auto__ 2]
(conj (conj [] x__10885__auto__) x__10885__auto__)))
;; gensym
(let* [x11095 1]
(let* [x11096 2]
(conj (conj [] x11095) x11096)))