views:

81

answers:

1

I'm a bit confused as to exactly when symbol capture will occur with clojure macros. Suppose that I have a macro which defines a function from keywords. In this trivial example,

(defmacro foo [keywd1 keywd2] `(defn ~(symbol (name keywd1)) 
                  [~(symbol (name keywd2))] (* 2 ~(symbol (name keywd2))))) 

I call (foo :bar :baz), and this gets expanded into (defn bar [baz] (* 2 baz)).

So now the question -- can this lead to symbol capture? If so, under what circumstances? I know that it's preferred to use gensym (e.g. bar#) to prevent symbol capture, but in some cases (not many, but still) I'd like to have a pretty macro-expansion, without the auto-generated symbols.

Bonus question: does the answer change if we are considering a macro that creates macros?

+3  A: 
kotarak
Jeremy Wall
Jeremy Wall: Your example won't work because bar will become namespace qualified once macroexpanded, causing the compiler to barf because you can't bind to a namespace qualified symbol.
Brian