From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Is it possible for a macro to expand to nothing?
Date: Tue, 24 Nov 2009 00:55:13 +0100 [thread overview]
Message-ID: <873a44dcf2.fsf@galatea.local> (raw)
In-Reply-To: mailman.11367.1259014174.2239.help-gnu-emacs@gnu.org
"Drew Adams" <drew.adams@oracle.com> writes:
>> > 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))
>
> You seem to be complaining about yourself. I never suggested using such a macro.
>
> All I suggested was to use backquote with ,@ to splice in a list with the
> element that Alan conditionally wants in the macroexpansion. That's the way to
> add something or nothing to a list: put the something or nothing in a list, then
> splice in that list.
>
> The list to be built here is a defun expression. It is then evaluated without
> problem.
>
>> 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.
>
> Sorry, dunno what you're saying. There wasn't anything invalid in what I
> suggested.
>
> As far as I'm concerned, all that's really involved is writing a macro that
> expands to a list that contains or doesn't contain the element in question. Any
> such list is a valid _list_.
This is the problem! Macros shouldn't return a _list_, they should
return a _form_. If you write a macro that returns a list, or you use
it so that it returns a list, that is not a valid form, then it is not
good style, even if you catch up.
> When the list resulting from macroexpansion is then EVALUATED, yes, of course
> its car must be a defined function, macro, `lambda', etc. (or the list must be
> nil). That is an entirely different matter. That is a consideration for _any_
> macro one writes.
>
> In Alan's case, the resulting list has `defun' as its car. It is a valid defun
> expression whose evaluation defines a function.
I don't mind titi, I object to your use of ifdef in titi. That ifdef
call returns a list that is not a valid form. This is not good style.
> The only thing relevant here, AFAICT, is how to create the list Alan wants: a
> list that conditionally contains some element. Backquote plus ,@ is the answer.
Yes, since you only want to create a list at that point, do not use a
macro, use a function.
>> 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)))
>
> Dunno why you do all that.
Because it is a better style. It avoids abusing the ifdef macro.
> (And there is no need to quote nil.)
There are again very good stylistic reasons to quote nil or ()
depending on the case.
> Again, all of that code boils down to just this (which is what I wrote earlier):
>
> (defmacro titi (fn)
> `(defun ,fn ()
> (setq bar 1)
> ,@(and baz '((setq bar 2)))))
I'm OK with this version (since it doesn't abuse the ifdef macro).
> I'm sure you know what you're talking about, and I know what I'm
> talking about ;-). The only real question is whether either of us
> has actually helped Alan at all.
Well, he has a lot of solutions to choose from now. Indeed, his
situation may not be better :-)
--
__Pascal Bourguignon__
next prev parent reply other threads:[~2009-11-23 23:55 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-23 14:56 Is it possible for a macro to expand to nothing? Alan Mackenzie
2009-11-23 16:03 ` Drew Adams
[not found] ` <mailman.11344.1258992201.2239.help-gnu-emacs@gnu.org>
2009-11-23 16:31 ` Alan Mackenzie
2009-11-23 17:29 ` Drew Adams
2009-11-23 18:33 ` Pascal J. Bourguignon
2009-11-23 18:51 ` Drew Adams
[not found] ` <mailman.11354.1259004470.2239.help-gnu-emacs@gnu.org>
2009-11-23 20:08 ` Pascal J. Bourguignon
2009-11-23 20:24 ` Alan Mackenzie
2009-11-23 22:09 ` Drew Adams
[not found] ` <mailman.11367.1259014174.2239.help-gnu-emacs@gnu.org>
2009-11-23 23:55 ` Pascal J. Bourguignon [this message]
2009-11-24 0:55 ` Alan Mackenzie
2009-11-24 9:42 ` Pascal J. Bourguignon
2009-11-24 10:45 ` Alan Mackenzie
2009-11-24 11:14 ` Pascal J. Bourguignon
2009-11-24 16:39 ` Alan Mackenzie
2009-11-24 19:17 ` Pascal J. Bourguignon
2009-11-25 14:13 ` Jeff Clough
[not found] ` <mailman.11467.1259158369.2239.help-gnu-emacs@gnu.org>
2009-11-26 6:53 ` Alan Mackenzie
2009-11-26 11:11 ` Pascal J. Bourguignon
2009-11-26 11:52 ` Lennart Borgman
[not found] ` <mailman.11564.1259236392.2239.help-gnu-emacs@gnu.org>
2009-11-26 12:16 ` Pascal J. Bourguignon
2009-11-26 12:43 ` Lennart Borgman
2009-11-27 8:32 ` Kevin Rodgers
[not found] ` <mailman.11626.1259310779.2239.help-gnu-emacs@gnu.org>
2009-11-27 13:15 ` Alan Mackenzie
2009-11-27 13:52 ` Pascal J. Bourguignon
2009-11-27 16:57 ` Alan Mackenzie
2009-11-27 17:09 ` Pascal J. Bourguignon
2009-11-27 17:19 ` Helmut Eller
2009-11-27 17:45 ` Pascal J. Bourguignon
2009-11-27 23:17 ` Tim X
2009-11-28 0:06 ` Pascal J. Bourguignon
2009-11-28 8:29 ` Alan Mackenzie
2009-11-28 10:25 ` Pascal J. Bourguignon
2009-11-28 12:57 ` Thierry Volpiatto
[not found] ` <mailman.11699.1259413441.2239.help-gnu-emacs@gnu.org>
2009-11-29 0:54 ` Pascal J. Bourguignon
2009-11-24 11:56 ` Pascal J. Bourguignon
[not found] ` <mailman.11352.1258997403.2239.help-gnu-emacs@gnu.org>
2009-11-23 18:42 ` Pascal J. Bourguignon
2009-11-23 20:12 ` Drew Adams
[not found] ` <mailman.11356.1259007263.2239.help-gnu-emacs@gnu.org>
2009-11-23 20:21 ` Pascal J. Bourguignon
2009-11-23 22:09 ` Drew Adams
[not found] ` <mailman.11368.1259014177.2239.help-gnu-emacs@gnu.org>
2009-11-24 0:03 ` Pascal J. Bourguignon
2009-11-23 20:09 ` Alan Mackenzie
2009-11-23 16:49 ` Jeff Clough
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=873a44dcf2.fsf@galatea.local \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).