views:

70

answers:

2

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 trivial and not macro-worthy, but it's the simplest example I could come up with.

+1  A: 

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.

Rayne
I think you're missing syntax-quote in both of those. (Also see my comment on the question.)
Michał Marczyk
Yeah, I forgot to add that. Sorry.
Rayne
A: 

What you probably want to do is this, using a Var, normal functions and binding:

(def x)
(defn foo [y] (dosync (alter x conj y)))
(defn bar [] (binding [x (ref [])]
               (foo 3)))
Michiel Borkent
No, I don't. My example was too simplistic. :p
Rayne