From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: "Garreau\, Alexandre" Newsgroups: gmane.emacs.devel Subject: pcase vs. case (where it could also be used) [Was: Re: Replace trivial pcase occurrences in the Emacs sources] Date: Tue, 23 Oct 2018 21:52:01 +0200 Message-ID: <87in1sjva6.fsf_-_@portable.galex-713.eu> References: <20151216202605.GA3752@acm.fritz.box> <56889EC3.3040108@yandex.ru> <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> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1540324210 31563 195.159.176.226 (23 Oct 2018 19:50:10 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 23 Oct 2018 19:50:10 +0000 (UTC) User-Agent: Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu, GTK+ Version 3.22.11) of 2017-09-15, modified by Debian Cc: emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Oct 23 21:50:06 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 1gF2gn-000858-Sv for ged-emacs-devel@m.gmane.org; Tue, 23 Oct 2018 21:50:06 +0200 Original-Received: from localhost ([::1]:43989 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gF2iu-0000AO-4H for ged-emacs-devel@m.gmane.org; Tue, 23 Oct 2018 15:52:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56390) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gF2in-00009n-0T for emacs-devel@gnu.org; Tue, 23 Oct 2018 15:52:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gF2il-0002JO-UR for emacs-devel@gnu.org; Tue, 23 Oct 2018 15:52:08 -0400 Original-Received: from portable.galex-713.eu ([2a00:5884:8305::1]:39156) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gF2il-0002II-MK for emacs-devel@gnu.org; Tue, 23 Oct 2018 15:52:07 -0400 Original-Received: from localhost ([::1] helo=portable.galex-713.eu) by portable.galex-713.eu with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gF2if-0006xb-9t; Tue, 23 Oct 2018 21:52:01 +0200 PGP-FINGERPRINT: E109 9988 4197 D7CB B0BC 5C23 8DEB 24BA 867D 3F7F Accept-Language: fr, en, eo, it, br In-Reply-To: (Stefan Monnier's message of "Tue, 23 Oct 2018 14:12:34 -0400") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:5884:8305::1 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:230609 Archived-At: On 2018-10-23 at 14:12, Stefan Monnier wrote: > In the specific case of using `pcase` where `cl-case` could also be > used, the downsides I know of `pcase` are the following: > - if you forget to quote your constant symbols the code behaves > differently (and arguably surprisingly for some user) rather than > giving you an error, tho in many cases the macro will detect a problem > and give you a warning about a subsequent redundant branch (but the > unsuspecting user will likely find it puzzling and it will take > a while for him to figure out what's going on). Aren=E2=80=99t non-quoted for bind? what should give an error or a warning?= I didn=E2=80=99t understood? Also, in either pcase or case: can=E2=80=99t they eval a symbol as a variab= le value to be used in tests? > [but] because it can also do a lot more, its doc is much more complex. > The advantages are: > - it can accommodate many more cases, so it offers a smoother evolution. > IOW, you will rarely find yourself having to stop using `pcase` and > to rewrite the code to use `cond` instead just because you need to > add a case that's a bit different from the others. To push the reasoning on an extreme: would that be a reason for abandoning cond, lambda, etc. in favor of pcase? ML do that, and I find it sad and less minimal and lightweight. > - once you learn it, you can use it elsewhere, e.g. pcase-let, > pcase-dolist, ... Could have been the same for cl-destructuring-bind. > - Elisp+pcase is simpler than Elisp+clcase+pcase, so if we aim for > overall simplicity we'd be better off without cl-case (to the extend > that `cl-case` is too limited and hence we'll still want to use > `pcase`). > the advantages I see for `cl-case`: You forgot simplicity and concision of implementation: easier to understand and reimplement, thus more hackable upon and widespread. For instance, I personally have my own case* implementation for using other test-fn than eql (such as equal, the only possible one for pcase, and I don=E2=80=99t even hope making my own version for eq/eql/etc.). Many people will want case anyway, probably much code uses it (including but not limited to fragments of common lisp afaik), so it=E2=80=99ll stay anyway, because it is a trivial and obvious factorization of cond, already existing in most languages, and simpler to make. While pcase is much more questionable, trivial, straightforward and widespread. elisp+clcase is simpler than elisp+pcase, overall. So for any codebase, one only needing case (more likely) will be simpler and have less dependency burden that one needing pcase. Also, from a more reductionist point of view: pcase can be implemented more simply and readably using case, typecase, etc. so that=E2=80=99s yet another argument for case to stay used and widespread. > - easier for people who already know Common-Lisp (or those who learned > `case` from the cl.el package). Also it is basically the equivalent of =E2=80=9Cswitch/case/etc.=E2=80=9D m= any will search for, coming from another language such as C: and not everybody is fond of destructuring and pattern matching > Here are the problems I see with cl-case (regardless of pcase): > - only works with numbers and symbols: can't use it to match against > strings. Can be fixed, and without breaking backward-compatibility. > - in (case X (a fooa)), the syntax looks a bit like a call to the > function `a`: emacs-lisp-mode gets confused in terms of highlighting > and completion, last I checked. case is a macro: why making assumption on calls in macros arguments? from time case known as a macro (I=E2=80=99ve a hard time imagining what a =E2=80=9Ccase=E2=80=9D *function* would be anyway)=E2=80=A6 Also, with an = unquoted symbol (so to bind the default case), you have this very same problem with pcase: (pcase X (a (fooa))) (and emacs syntax highlighting is confused here too: (pcase X (cond (fooa))), (pcase X (function (fooa))). So emacs-lisp-mode would better be fixed at some point rather than macros arbitrarily disapproved. Then: > - [pcase] fixes all four problems I described about `cl-case`. hence not even. > - tricky corner cases when trying to match the `t` or `nil` values (or > `otherwise` as well, tho this one is much more rarely needed). I find these interface details sadening, it would have been more useful the other way. Btw as said before, matching the default case can be considered confusing as well with pcase. > - some users naturally write (case X ('a fooa) ...) without realizing > that it also matches thew `quote` value. I find sad cl case doesn=E2=80=99t eval its arguments :/ The two last issue would be lessened it it was this way btw. But doing otherwise would be incompatible with cl, with which I find neat emacs lisp is partially compatible with, and it would break backward-compatibility, and much code (as much as it would to remove it at all).