From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Adam Porter Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] pcase.el: Add type pattern Date: Tue, 10 Mar 2020 12:03:40 -0500 Message-ID: <87o8t487mb.fsf@alphapapa.net> References: <874kuxxuez.fsf@alphapapa.net> <87r1y1wcj4.fsf@alphapapa.net> <87h7yxw5x1.fsf@alphapapa.net> <87a74puq1q.fsf@web.de> <87d09lw3id.fsf@alphapapa.net> <87zhcoqmmz.fsf@web.de> <87wo7s8b73.fsf@alphapapa.net> <87eeu0qisn.fsf@web.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="74596"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Mar 10 18:04:52 2020 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 1jBiJI-000JHi-Kb for ged-emacs-devel@m.gmane-mx.org; Tue, 10 Mar 2020 18:04:52 +0100 Original-Received: from localhost ([::1]:37178 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBiJH-0008Or-MX for ged-emacs-devel@m.gmane-mx.org; Tue, 10 Mar 2020 13:04:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33371) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBiIN-0007A0-Az for emacs-devel@gnu.org; Tue, 10 Mar 2020 13:03:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jBiIL-0000ir-TX for emacs-devel@gnu.org; Tue, 10 Mar 2020 13:03:55 -0400 Original-Received: from ciao.gmane.io ([159.69.161.202]:52102) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jBiIL-0000e5-LJ for emacs-devel@gnu.org; Tue, 10 Mar 2020 13:03:53 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1jBiIG-000HpF-KT for emacs-devel@gnu.org; Tue, 10 Mar 2020 18:03:48 +0100 X-Injected-Via-Gmane: http://gmane.org/ X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 159.69.161.202 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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" Xref: news.gmane.io gmane.emacs.devel:245433 Archived-At: --=-=-= Content-Type: text/plain Michael Heerdegen writes: > Adam Porter writes: > >> +(pcase-defmacro type (type) >> + "Pcase pattern that matches objects of TYPE. >> +TYPE is a symbol or list as accepted by `cl-typep', which see." >> + `(pred (pcase--flip cl-typep ',type))) >> + > > Is there a reason why you omit the (require 'cl-lib) call in the > definition as suggested by Stefan? My understanding was that adding the (require 'cl-lib) form was suggested if the definition was left in pcase.el, and if the definition were added to cl-macs.el instead, the (require 'cl-lib) form would not be needed. This made sense to me, because cl-macs.el has (require 'cl-lib) at the top of the file. Of course, I may have misunderstood, and there are nuances to autoloading and such that I don't fully understand. > With your definition in emacs -Q: > > (macroexpand-all '(pcase 10 ((type (integer 0 10)) t))) => > (if (cl-typep 10 '(integer 0 10)) (progn t) nil) > > With Stefan's version: > > (macroexpand-all '(pcase 10 ((type (integer 0 10)) t))) => > (if (and (integerp 10) (>= 10 '0) (<= 10 '10)) (progn t) nil) > > I.e. the type expression is treated at compile time and the dependency > to cl-lib is only at compile time, the compiled code has no dependency. > That should be better, no? Of course, that would be better. However, I was unable to reproduce those results on my end. Here are the steps I followed, both on my main Emacs 26.3 installation and on a build from near master: 1. Run emacs -Q. 2. Evaluate this form in *scratch*: (pcase-defmacro type (type) "Pcase pattern that matches objects of TYPE. TYPE is a symbol or list as accepted by `cl-typep', which see." `(pred (pcase--flip cl-typep ',type))) 3. Evaluate this form in *scratch*: (macroexpand-all '(pcase 10 ((type (integer 0 10)) t))) Result: (if (and (integerp 10) (>= 10 (quote 0)) (<= 10 (quote 10))) (progn t) nil) I haven't done much work on Emacs's core files before, so I may be doing something wrong. Please let me know. :) However, I have attached another version of the patch which adds an autoload for the (pcase-defmacro type) form, which seems proper since the (pcase-defmacro cl-struct) form in the same file also has one. Thanks for your help and feedback. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-lisp-emacs-lisp-cl-macs.el-Add-type-pattern.patch Content-Description: Add pcase type pattern, etc. >From 3ab1eae0e71a495bb4e86012ad772146454aa4ff Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Mon, 9 Mar 2020 13:01:32 -0500 Subject: [PATCH] * lisp/emacs-lisp/cl-macs.el: Add type pattern * lisp/emacs-lisp/cl-macs.el: ((pcase-defmacro type)): Add 'type' pattern. * test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-type): Add test. * doc/lispref/control.texi (pcase Macro): Update manual. Co-authored-by: Stefan Monnier --- doc/lispref/control.texi | 7 +++++++ etc/NEWS | 8 ++++++++ lisp/emacs-lisp/cl-macs.el | 8 ++++++++ test/lisp/emacs-lisp/pcase-tests.el | 10 ++++++++++ 4 files changed, 33 insertions(+) diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index c601e3a..dc91fc1 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi @@ -555,6 +555,13 @@ pcase Macro Likewise, it makes no sense to bind keyword symbols (@pxref{Constant Variables}). +@item (type @var{type}) +Matches if @var{expval} is of type @var{type}, which is a symbol or +list as accepted by @ref{cl-typep,,,cl,Common Lisp Extensions}. + +Example: @code{(type integer)} +Example: @code{(type (integer 0 10))} + @item (pred @var{function}) Matches if the predicate @var{function} returns non-@code{nil} when called on @var{expval}. diff --git a/etc/NEWS b/etc/NEWS index 47b87af..c8227c0 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -174,6 +174,14 @@ key binding / v package-menu-filter-by-version / / package-menu-filter-clear +** pcase.el + +*** Added Pcase 'type' pattern. + +The new 'type' pattern compares types using 'cl-typep', which allows +comparing simple types like '(type integer)', as well as forms like +'(type (integer 0 10))'. + * New Modes and Packages in Emacs 28.1 diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index 4c2f589..556ffb3 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -3403,6 +3403,14 @@ cl-struct-slot-value (run-hooks 'cl-macs-load-hook) +;;; Pcase type pattern. + +;;;###autoload +(pcase-defmacro type (type) + "Pcase pattern that matches objects of TYPE. +TYPE is a symbol or list as accepted by `cl-typep', which see." + `(pred (pcase--flip cl-typep ',type))) + ;; Local variables: ;; generated-autoload-file: "cl-loaddefs.el" ;; End: diff --git a/test/lisp/emacs-lisp/pcase-tests.el b/test/lisp/emacs-lisp/pcase-tests.el index 0b69bd9..97f3e55 100644 --- a/test/lisp/emacs-lisp/pcase-tests.el +++ b/test/lisp/emacs-lisp/pcase-tests.el @@ -71,6 +71,16 @@ pcase-tests-member (ert-deftest pcase-tests-vectors () (should (equal (pcase [1 2] (`[,x] 1) (`[,x ,y] (+ x y))) 3))) +(ert-deftest pcase-tests-type () + (should (equal (pcase 1 + ((type integer) 'integer)) + 'integer)) + (should (equal (pcase 1 + ((type (integer 0 2)) 'integer-0<=n<=2)) + 'integer-0<=n<=2)) + (should-error (pcase 1 + ((type notatype) 'integer)))) + ;; Local Variables: ;; no-byte-compile: t ;; End: -- 2.7.4 --=-=-=--