[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Macro expansion: Why doesn't the invoked macro see (let (variables))
Re: Macro expansion: Why doesn't the invoked macro see (let (variables))from the invoking one?
Wed, 8 Feb 2012 20:09:08 +0000
On Wed, Feb 08, 2012 at 02:52:45PM -0500, Stefan Monnier wrote:
> > (defmacro run-hooks-here ()
> > (setq hooks-called t) <================= flag variable
> > `(run-hooks ',hook (if ,mode ',hook-on ',hook-off)))
> > (defmacro define-minor-mode (....)
> > ....
> > (let (... hooks-run)
> > ....
> > ,@body <================= expand invoker's forms
> This comment is wrong: ",@body" just plugs in the `body' without
> macro-expanding it.
> > <====== There may be (run-hooks-here) here.
> > ,@(unless hooks-run `((run-hooks-here))) <========= test flag
> You can do that, but you then need to make sure the `body' gets
> macro-expanded while the `let' is live, i.e. during the expansion of the
> call to `define-minor-mode'.
Surely it should be done that way anyway? I think I'm beginning to get a
clue, but why would expansion of macros be delayed? That seems to
preclude normal healthy use of side effects.
> You can do it with something like
> (defmacro define-minor-mode (....)
> (let (... hooks-run)
> ,@(macroexpand-all body) <================= expand invoker's forms
> <====== There may be (run-hooks-here) here.
> ,@(unless hooks-run `((run-hooks-here))) <========= test flag
> But note that this counts as ugly.
I wouldn't disagree with that.
> We use such tricks in cl-macs.el to figure out whether `body' uses
> `return-from' within a `block' (in order to optimize away the `catch'
> that's otherwise needed), but it's ugly, inefficient, and brittle. An
> :after-hook (or :late-code or some other name you prefer) is much
> better in this regard.
I think you're right. Ah well. I learnt this afternoon that quoting a
doesn't stop it being expanded. I don't think that's in the elisp
Alan Mackenzie (Nuremberg, Germany).
Re: Macro expansion: Why doesn't the invoked macro see (let (variables)) from the invoking one?, Tassilo Horn, 2012/02/08