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: Alan Mackenzie
Subject: Re: Is it possible for a macro to expand to nothing?
Date: Mon, 23 Nov 2009 20:24:47 +0000 (UTC)
User-agent: tin/1.6.2-20030910 ("Pabbay") (UNIX) (FreeBSD/4.11-RELEASE (i386))

Pascal J. Bourguignon <address@hidden> wrote:
> "Drew Adams" <address@hidden> writes:

>>> >> you can do that by producing nil and then using ,@ inside a
>>> >> backquote. IOW, instead of inserting nil, you splice it in,
>>> >> which means inserting nothing.

>>> That wouldn't work, backquote is a quote.

>> Sorry, but it works just fine. I do this all the time.

> No, it doesn't:

It worked fine for me.

> (defmacro ifdef (expr &rest body)
>   (and (eval expr) `(progn ,@body)))

My definition of `ifdef' is:

    (defmacro ifdef (condition form)
      (if (eval condition)
          `,form))

, which though lacking an &rest argument, could easily be enhanced to
take one.

[ .... ]

> I didn't say that it didn't work inside titi.  I said that it was a bad
> idea to take the habit of giving an invalid form to a macro to get an
> invalid form from it.

If the form doesn't cause a runtime error, it's not invalid.  ;-) The
reason I want this functionality is to optimise a VERY VERY tight loop.
I believe that the optimisation isn't feasible under XEmacs, so I want to
be able to put things for XEmacs inside Lisp equivalents of "#ifdef".
Even inserting an invocation of `(ignore)' seems to make a difference.
As far as I remember, anyway.  With Drew's mechanism, one doesn't have to
rely on the byte-compiler optimising an empty `progn' or a frivolous
`progn' away.

> Notice also my alternative solution uses macroexpand.   This is a clue
> that if you want to go that way, you should use a function rather than
> a macro:

>   (defun %parenthesized-ifdef (expr forms)
>      (if expr
>          '()
>          `((progn ,@forms))))

>   (defmacro titi (fn)
>      `(defun ,fn  ()
>          (setq bar 1)
>          ,@(%parenthesized-ifdef baz '((setq bar 2)))))

> (macroexpand '(titi foo))
> --> (defun foo nil (setq bar 1) (progn (setq bar 2)))

Yes, but that `progn' is ugly.

-- 
Alan Mackenzie (Nuremberg, Germany).



reply via email to

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