[Top][All Lists]

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

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

From: Pascal J. Bourguignon
Subject: Re: Is it possible for a macro to expand to nothing?
Date: Mon, 23 Nov 2009 21:08:04 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

"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:

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

(ifdef t ((setq bar 2)))
Debugger entered--Lisp error: (invalid-function (setq bar 2))
  ((setq bar 2))
  (progn ((setq bar 2)))
  (ifdef t ((setq bar 2)))
  eval((ifdef t ((setq bar 2))))

> You snipped out the code I suggested. Here it is again, with `and' substituted
> for `ifdef' (which is undefined).
> (defmacro titi (fn)

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.

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)))

__Pascal Bourguignon__

reply via email to

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