(elisp)Eval During Expansion

Next: Repeated Expansion Prev: Surprising Local Vars Up: Problems with Macros

Evaluating Macro Arguments in Expansion

   Another problem can happen if you evaluate any of the macro argument
expressions during the computation of the expansion, such as by calling
`eval' (Note: Eval.).  If the argument is supposed to refer to the
user's variables, you may have trouble if the user happens to use a
variable with the same name as one of the macro arguments.  Inside the
macro body, the macro argument binding is the most local binding of this
variable, so any references inside the form being evaluated do refer to
it.  Here is an example:

     (defmacro foo (a)
       (list 'setq (eval a) t))
          => foo
     (setq x 'b)
     (foo x) ==> (setq b t)
          => t                  ; and `b' has been set.
     ;; but
     (setq a 'b)
     (foo a) ==> (setq 'b t)     ; invalid!
     error--> Symbol's value is void: b

   It makes a difference whether the user types `a' or `x', because `a'
conflicts with the macro argument variable `a'.

   In general it is best to avoid calling `eval' in a macro definition
at all.

automatically generated by info2www