help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: Is it possible for a macro to expand to nothing?


From: Drew Adams
Subject: RE: Is it possible for a macro to expand to nothing?
Date: Mon, 23 Nov 2009 12:12:59 -0800

> > (defmacro titi (fn)
> >   `(defun ,fn ()
> >      (setq bar 1)
> >      ,@(ifdef baz '((setq bar 2))))))
> >
> > Assuming that ifdef returns nil if baz is nil, that should 
> > give you (defun foo () (setq bar 1)). If baz is not nil,
> > it should give you this:
> >
> > (defun foo ()
> >  (setq bar 1)
> >  (setq bar 2))
> 
> Yes.  Unfortunately, (ifdef baz '((setq bar 2))) doesn't produce a
> valid form when baz is not nil.

Huh? It produces the list ((setq bar 2)), assuming `ifdef' acts like `and'
(`ifdef' is undefined AFAIK).

((setq bar 2)) is then spliced in, because of ,@.
And that produces the requested code.

> When it is used outside of a macro, that produces an error.

'((setq bar 2)) produces the list ((setq bar 2)) anywhere you evaluate it.

> This is not a good property.  It would be
> better to keep the contract of macros, that is they take code, and
> they produce code, that is, valid forms.

No idea what you are talking about.

It's simple, really: The code resulting from macroexpansion is a list. Just
splice a list into the list you are building, instead of adding an element to
it. If the list you splice in is (), then nothing is added to the list.

(setq a1 '(M)  a2 ())
(macroexpand `(x ,@a1)) = (x M)
(macroexpand `(x ,@a2)) = (x)

That's really all there is to it: instead of trying to add or not add M, splice
in (M) or ().





reply via email to

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