0

Suppose I have defined the following macro:

(defmacro mymacro (a) "" (eval a))

If I then use eval-last-sexp (i.e. C-x C-e) to evaluate the following,

(defun somefcn (b) "" (mymacro b))

I get the error (void-variable b). However, if I evaluate the definition another way (e.g. using eval-expression), it evaluates fine and works fine.

Note that the following expression evaluates without a problem

(defun somefcn (a) "" (mymacro a))

What is going on?

3

After looking through the GNU Emacs Lisp Reference Manual, I realized that there is a subsection of "Common Problems Using Macros" called "Evaluating Macro Arguments in Expansion" which explains this issue.

The basic problem is that, when a macro is compiled as opposed to interpreted, the first evaluation of the macro is inserted into function definitions in place of the macro. Thus, my macro is actually calling eval on the not-defined variable b, hence returning an error. Basically, one should avoid using eval in the initial part of macro evaluations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.