In Section 13.5.4 of the Elisp reference, we are shown a problem whereby a macro definition evaluates one of its argument expressions.
(defmacro foo (a)
(list 'setq (eval a) t))
(setq x 'b)
(foo x) → (setq b t)
⇒ t ; and b has been set.
;; but
(setq a 'c)
(foo a) → (setq a t)
⇒ t ; but this set a, not c.
We are then advised to
substitute the expression into the macro expansion, so that its value will be computed as part of executing the expansion.
I'm not sure what this means, but I interpret it as meaning that we should try to rewrite the macro as follows:
(defmacro foo (a)
`(setq (eval ',a) t))
Which, when expanded, gives:
(macroexpand '(foo x)) → (setq (eval (quote x)) t)
But I'm getting the following error when I call (foo x)
:
(wrong-type-argument symbolp (eval (quote x)))
Even though when I evaluate (symbolp (eval (quote x)))
, the result is t
.
It seems that there's something fundamental that I'm not understanding here. What's the issue?
a
, notc
. (But there is a typo: "set" should be "sets".)