From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: Is it possible for a macro to expand to nothing? Date: Mon, 23 Nov 2009 14:09:11 -0800 Message-ID: <24AB7C55C06E45238BC3FC5FFD983E97@us.oracle.com> References: <87vdh1ccra.fsf@galatea.local> <87my2dc8d7.fsf@galatea.local> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1259014420 27429 80.91.229.12 (23 Nov 2009 22:13:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 23 Nov 2009 22:13:40 +0000 (UTC) To: "'Pascal J. Bourguignon'" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Nov 23 23:13:33 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NChA3-0002ZY-Kr for geh-help-gnu-emacs@m.gmane.org; Mon, 23 Nov 2009 23:13:31 +0100 Original-Received: from localhost ([127.0.0.1]:59784 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NChA3-0008V3-5R for geh-help-gnu-emacs@m.gmane.org; Mon, 23 Nov 2009 17:13:31 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NCh6D-0006T7-3V for help-gnu-emacs@gnu.org; Mon, 23 Nov 2009 17:09:33 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NCh66-0006PB-TN for help-gnu-emacs@gnu.org; Mon, 23 Nov 2009 17:09:31 -0500 Original-Received: from [199.232.76.173] (port=58956 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NCh66-0006Ou-Nc for help-gnu-emacs@gnu.org; Mon, 23 Nov 2009 17:09:26 -0500 Original-Received: from acsinet12.oracle.com ([141.146.126.234]:42563) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NCh66-0006Gy-7w for help-gnu-emacs@gnu.org; Mon, 23 Nov 2009 17:09:26 -0500 Original-Received: from rgminet15.oracle.com (rcsinet15.oracle.com [148.87.113.117]) by acsinet12.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id nANM9LZK000821 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 23 Nov 2009 22:09:22 GMT Original-Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by rgminet15.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id nANM9MDu007720; Mon, 23 Nov 2009 22:09:23 GMT Original-Received: from abhmt007.oracle.com by acsmt358.oracle.com with ESMTP id 551715321259014149; Mon, 23 Nov 2009 16:09:09 -0600 Original-Received: from dradamslap1 (/130.35.178.194) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 23 Nov 2009 14:09:09 -0800 X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <87my2dc8d7.fsf@galatea.local> Thread-Index: AcpsgCxmddlP3zc1QUOoIPXR0QlTTQAAVVOg X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Source-IP: acsmt357.oracle.com [141.146.40.157] X-Auth-Type: Internal IP X-CT-RefId: str=0001.0A090207.4B0B0811.003B:SCFMA4539814,ss=1,fgs=0 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 1) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:70064 Archived-At: > > 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_. 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. 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. > 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. Just `,@forms is as useful here as `((progn ,@forms)) - a defun body is an implicit progn. (And there is no need to quote nil.) 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 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.