From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: Replace trivial pcase occurrences in the Emacs sources Date: Thu, 25 Oct 2018 17:47:18 +0300 Message-ID: <83r2geqe15.fsf@gnu.org> References: <20151216202605.GA3752@acm.fritz.box> <877fjrkpdf.fsf@fencepost.gnu.org> <56892334.4000106@yandex.ru> <8760zakb7q.fsf@fencepost.gnu.org> <56892BDA.6060103@dancol.org> <871t9yk98g.fsf@fencepost.gnu.org> <568936F0.3060505@yandex.ru> <87wprqitj5.fsf@fencepost.gnu.org> <56893C8C.3060200@yandex.ru> <87oad2irtd.fsf@fencepost.gnu.org> <5689456A.1010601@yandex.ru> <87egdy8tyz.fsf@fencepost.gnu.org> <56895FDE.4060406@yandex.ru> <8760za8r4a.fsf@fencepost.gnu.org> <87h9iunkcg.fsf@web.de> <87h8hc4xw2.fsf_-_@web.de> <83tvlcsnee.fsf@gnu.org> <87pnw037ar.fsf@web.de> <83ftwvs7y9.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1540482273 32600 195.159.176.226 (25 Oct 2018 15:44:33 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 25 Oct 2018 15:44:33 +0000 (UTC) Cc: emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Oct 25 17:44:29 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gFhoB-0008Jn-4n for ged-emacs-devel@m.gmane.org; Thu, 25 Oct 2018 17:44:27 +0200 Original-Received: from localhost ([::1]:55273 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFhqH-0001V5-Az for ged-emacs-devel@m.gmane.org; Thu, 25 Oct 2018 11:46:37 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60234) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFh5S-00045Y-Dj for emacs-devel@gnu.org; Thu, 25 Oct 2018 10:58:15 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFguw-0007wv-Pk for emacs-devel@gnu.org; Thu, 25 Oct 2018 10:47:25 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:44043) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFguw-0007wF-HK; Thu, 25 Oct 2018 10:47:22 -0400 Original-Received: from [176.228.60.248] (port=2027 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1gFguu-0006JB-S5; Thu, 25 Oct 2018 10:47:22 -0400 In-reply-to: (message from Stefan Monnier on Wed, 24 Oct 2018 16:52:23 -0400) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:230659 Archived-At: > From: Stefan Monnier > Date: Wed, 24 Oct 2018 16:52:23 -0400 > > > (pcase this-param > > ('edit (todo-edit-item--text)) > > ('header (todo-edit-item--text 'include-header)) > > ('multiline (todo-edit-item--text 'multiline)) > > ('add/edit (todo-edit-item--text 'comment-edit)) > > ('delete (todo-edit-item--text 'comment-delete)) > > ('diary (todo-edit-item--diary-inclusion)) > > ('nonmarking (todo-edit-item--diary-inclusion 'nonmarking)) > > [...] > > Is the below any better? > > (cond > ((eq this-param 'edit) (todo-edit-item--text)) > ((eq this-param 'header) (todo-edit-item--text 'include-header)) > ((eq this-param 'multiline) (todo-edit-item--text 'multiline)) > ((eq this-paran 'add/edit) (todo-edit-item--text 'comment-edit)) > ((eq this-parom 'delete) (todo-edit-item--text 'comment-delete)) > ((eq this-param 'diary) (todo-edit-item--diary-inclusion)) > ((eq this-param 'nonmarking) (todo-edit-item--diary-inclusion 'nonmarking)) > [...] Yes (modulo the typos). > To me, it's more verbose and more complex because you need to double > check that the same var is tested each time before you can know that it's > equivalent to a C-style `switch`. But it isn't equivalent to C-style 'switch': it doesn't compare like C does, and it doesn't dispatch like C does. (I find it generally not useful to think in C concepts when programming in Lisp, because it might cause confusion and mistakes.) > IOW, I consider rewriting the `cond` to use `pcase` to be a form of > "common sub-expression elimination", or reduction of copy&paste. Unfortunately, it makes the code more subtle and less self-explanatory. Not for you and me, perhaps, but for quite a few people who aren't as familiar with these macros, it seems. > I think rewriting those pcase uses into cond+eq would be equivalent for > me to rewriting dolists into while+pop loops IMO, it isn't equivalent, because dolist doesn't invent new syntax, not as much as pcase does. > I prefer pcase to cl-case but I even much more strongly prefer cl-case > over cond+eq. The thing is, both cl-case and pcase are subtly different from the "boring" cond, and from each other. E.g., cl-case uses eql to compare, so works with floats, but not with strings; pcase decides what predicate to use according to the data, so works with strings, but strangely doesn't work with floats. Again, perhaps this is not a problem for you and me, but newcomers might well stumble on these subtleties, and at the very least will have to consult the docs to know for sure. By contrast, with cond everything is explicit and self-explanatory. The programmer decides which equality to use. Yes, that does come for a price, but copy-yanking is cheap and fast, as is "M-/", and also avoids typos. > We clearly have very different programming backgrounds: to me the "case > style" is much nicer and easier to read, closer to what I think in > my head, whereas the "cond+eq style" is like a "assembly-language" > version of the same. This isn't about style, this is about using macro facilities to significantly change the syntax of a language. E.g., what do you think about people who use cpp to define macros BEGIN and END that expand into braces? One could argue that it is "closer to what one thinks in their head", or being "higher-level", and yet such practices are generally discouraged. > > (Quick: what's the difference between `require and 'require in this > > case?) > > Same difference as between 'require and `require in normal Elisp code. > Why is that a problem in pcase and not in the rest of Elisp? Because people stumble on this and are likely to waste time trying to understand what kind of magic hides behind this. Worse, they might copy this without understanding (as we already see in our sources), thus proliferating the obfuscation. Mind you: I'm not against pcase where its power is needed. It is IMO needed where using the underlying core facilities would produce something that is hard to follow and even harder to understand. Simple comparisons against a collection of fixed values doesn't fit that bill, IMO.