emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Macro expansion: Why doesn't the invoked macro see (let (variables))


From: Drew Adams
Subject: RE: Macro expansion: Why doesn't the invoked macro see (let (variables))from the invoking one?
Date: Wed, 8 Feb 2012 09:46:45 -0800

> (defmacro BAR ()
>   (message (if (boundp 'asdf) "asdf" "no asdf"))
>   '(message "bar"))
> 
> (defmacro FOO () (let (asdf) `(BAR)))
>
> One macro FOO binds a let variable, then invokes another 
> macro BAR.  BAR doesn't see this let variable.  Why not?

That `let' binding is evaluated only when FOO is expanded.  It is not part of
the resulting expansion (which is then evaluated).

> Is there anything I can do about this?

1. Don't use side effects in the macro definition.

A macro just produces a first result (the expansion), which is then evaluated to
produce the final result.  Anything you want to be seen during that latter
evaluation needs to be part of the expansion itself.

The first call to `message' in macro BAR has no effect when the expansion of BAR
is evaluated (it is not part of the expansion).  `(BAR)' is expanded to
`(message "bar")', which is then evaluated to "bar", which is returned.

2. You might be looking for something like this (dunno):

(defmacro FOO () `(let (asdf) ,(BAR)))

When `(FOO)' is expanded, the expansion includes a `let' binding.

This is the result of `(macroexpand '(FOO))':

(let (asdf) "bar")

Not sure what you're really trying to do, though.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]