From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.devel Subject: Re: Why shouldn't we have a #if .... #else .... #endif construct in Emacs Lisp? Date: Tue, 29 Aug 2023 13:23:59 +0000 Message-ID: References: <877cpeko06.fsf@posteo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16263"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Philip Kaludercic Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Aug 29 15:24:53 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qayiD-00040U-MT for ged-emacs-devel@m.gmane-mx.org; Tue, 29 Aug 2023 15:24:53 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qayhX-0006tF-L8; Tue, 29 Aug 2023 09:24:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qayhW-0006qg-BY for emacs-devel@gnu.org; Tue, 29 Aug 2023 09:24:10 -0400 Original-Received: from mail.muc.de ([193.149.48.3]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qayhR-00041O-VV for emacs-devel@gnu.org; Tue, 29 Aug 2023 09:24:09 -0400 Original-Received: (qmail 35074 invoked by uid 3782); 29 Aug 2023 15:24:01 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=muc.de; i=@muc.de; q=dns/txt; s=default; t=1693315441; h=date : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to : from : from; bh=buy4MuidCu8Mr9CcOr0BhnYEFJ4tjBbc5tQSs8bRuCk=; b=RBcrMLc+rQtKKQFc578tnrPVE260sIK0ST6lJrHuhnhX9YzlkCgsw7mY250eb5nZHW8eJ at4rq8nmPvaGR3YWJNjRxxlJQ12601qvRH5bvlvvV+i/rRzZ3+sI/DXFME9/aZLCahyoXAo bpl2xw76f5pPVwY2ZErL1/qiZLU7hCTdmxWUAnGtz2MANbjKpz7hcbaiVmd1pEqCK8pgElw 4Qg8Is4ieLEM0D9r/M0rTYEsaTxmd+b+OTw4f88wCKm24wkdws4RfZ6OJBGtKgeH93FU4Pb tNZMHT+M1X/xk6qu1GCK9q9ft27Lxj8n7CXyON9JnMuQc7xe7/c6qg44cICg== Original-Received: from acm.muc.de (p4fe15a05.dip0.t-ipconnect.de [79.225.90.5]) (using STARTTLS) by colin.muc.de (tmda-ofmipd) with ESMTP; Tue, 29 Aug 2023 15:23:59 +0200 Original-Received: (qmail 18339 invoked by uid 1000); 29 Aug 2023 13:23:59 -0000 Content-Disposition: inline In-Reply-To: <877cpeko06.fsf@posteo.net> X-Submission-Agent: TMDA/1.3.x (Ph3nix) X-Primary-Address: acm@muc.de Received-SPF: pass client-ip=193.149.48.3; envelope-from=acm@muc.de; helo=mail.muc.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:309505 Archived-At: Hello, Philip. On Tue, Aug 29, 2023 at 12:54:17 +0000, Philip Kaludercic wrote: > Alan Mackenzie writes: > > Hello, Emacs. > > In C, we have the very useful conditional compilation directives > > introduced by #if or #ifdef, etc., which end at #end. > > In Emacs Lisp we have no such construct. This is a Bad Thing. > > More and more, especially recently, irritating warning messages are > > occurring for, for example, obsolete variables and functions inside > > conditionals which ensure they aren't used. For example: > > (when (< emacs-major-version 24) > > (defadvice .....)) > > produces the warning about defadvice being obsolete. (I haven't actually > > tested this example). What we really want here is for the defadvice only > > to be _compiled_ when (< emacs-major-version 24), rather than compiled > > unconditionally and not run. > In this specific case, would it be possible to use the nadvice > compatibility package on GNU ELPA? I suspect it would be, yes. > > I propose a new function, hash-if, which would do what we want. The > > above example could then be written something like: > > (hash-if (< emacs-major-version 24) > > (defadvice .....) > > (advice-add .....)) > > .. This is not actually all that difficult to write. My first attempt > > uses a compiler-macro, and looks like this: > > (defun hash-if (condition if-part &rest else-part) > > "A compiler macro analogous to C's #if. > > CONDITION is evaluated at compile time. If it is non-nil, > > IF-PART gets compiled. Otherwise ELSE-PART (enclosed in a > > `progn') gets compiled." > > (declare (indent 2)) > > (error "hash-if has been called directly")) > > (put 'hash-if 'compiler-macro > > (lambda (form condition if-part &rest else-part) > > (if (eval condition lexical-binding) > > if-part > > (cons 'progn else-part)))) > Would something like work as well: > --8<---------------cut here---------------start------------->8--- > (defmacro cif (test then &rest else) > "Evaluate TEST during macro-expansion and return THEN or ELSE." > (if (eval test t) then else)) > --8<---------------cut here---------------end--------------->8--- Hah! Trust me to build something twisted and complicated when there's a simple solution which will do just as well! I think the `else' needs a 'progn, though, like this: (if (eval test t) then (cons 'progn else)) , and the eval form probably wants to go into a condition-case for Emacsen lacking the second parameter. But I appreciate you put the code together quickly, rather than working out every last detail. Thanks! > > .. I propose adding it to subr.el, just before (defmacro when ....). > > What do people think about this? Yes, what do people think about the idea? -- Alan Mackenzie (Nuremberg, Germany).