开发者

Accessing a local from one macro in another executed in the scope of the let

开发者 https://www.devze.com 2022-12-28 16:01 出处:网络
Be gentle, as my macrofoo is weak. What I\'d like to do is something like this: (defmacro foo [x] `(dosync (alter x# conj x)))

Be gentle, as my macrofoo is weak.

What I'd like to do is something like this:

(defmacro foo [x] `(dosync (alter x# conj x)))
(defmacro bar [] `(let [x# (ref [])] (foo 3)))

Is this possible? I can't just (let [x ..] ..) because of symbol capturing.

NOTE: I'm aware this example is开发者_JAVA百科 trivial and not macro-worthy, but it's the simplest example I could come up with.


I needed to force symbol capturing.

(defmacro foo [x] `(dosync (alter ~'x conj x)))
(defmacro bar [] `(let [~'x (ref [])] (foo 3)))

Although dangerous if you don't understand macros thoroughly, this works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号