From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Adam Porter Newsgroups: gmane.emacs.devel Subject: [PATCH] pcase.el: Add cl-type and type patterns Date: Sat, 10 Jul 2021 20:33:24 -0500 Message-ID: <87eec53a8r.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> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30336"; 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 Sun Jul 11 03:34:21 2021 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 1m2OMO-0007gv-SX for ged-emacs-devel@m.gmane-mx.org; Sun, 11 Jul 2021 03:34:20 +0200 Original-Received: from localhost ([::1]:36380 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m2OMN-0006wI-Tj for ged-emacs-devel@m.gmane-mx.org; Sat, 10 Jul 2021 21:34:19 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33246) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m2OLk-0006Fv-HA for emacs-devel@gnu.org; Sat, 10 Jul 2021 21:33:40 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]:43188) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m2OLh-0002FH-BK for emacs-devel@gnu.org; Sat, 10 Jul 2021 21:33:40 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1m2OLd-0006hf-Ow for emacs-devel@gnu.org; Sun, 11 Jul 2021 03:33:33 +0200 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=ged-emacs-devel@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.001, SPF_HELO_NONE=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.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:271172 Archived-At: --=-=-= Content-Type: text/plain Hi Stefan, et al, Stefan Monnier writes: >> Thanks, that's a good point. This new patch improves the docstring >> and the changes to the manual, and it adds a test for a cl-typep list >> form. Please let me know if more improvements should be made. > > The code looks good, except for the fact that it only works after > (require 'cl-lib), so if we want to put it into `cl-lib` like that, we > should use a name like `cl-type` rather than `type`. Revisiting this patch from last year (thread here: ): I'm attaching two new patches, rebased on current master: 1. The first implements the new cl-type-based matcher, renamed to `cl-type' as requested. 2. The second applies on top of the first and, like the initial patch proposed in the thread, adds a "dumb" `type' matcher which just uses a `TYPEp' or `TYPE-p' predicate as appropriate. My rationale for also adding this matcher is for writing concise code: with only the cl-type matcher, the equivalent `pred' matcher is shorter: (pred integerp) (cl-type integer) But by also adding `type', for simple types, we can do better, having the best of clarity and conciseness: (type integer) Since it was decided that the cl-type-based matcher was preferable as an alternative to the "dumb" matcher (due to cl-type's extra power, and its correctness in matching structs defined with custom predicate names), I propose this as a separate patch; if having both is not acceptable, the second patch may be omitted. Thanks, Adam --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-lisp-emacs-lisp-cl-macs.el-Add-cl-type-pattern.patch Content-Description: cl-type-based pcase matcher >From d24ca51e670ca8d49e7caae377eeebb528211caf Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Mon, 9 Mar 2020 13:01:32 -0500 Subject: [PATCH 1/2] * lisp/emacs-lisp/cl-macs.el: Add cl-type pattern * lisp/emacs-lisp/cl-macs.el: ((pcase-defmacro type)): Add 'cl-type' pattern. * test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-cl-type): Add test. * doc/lispref/control.texi (pcase Macro): Update manual. With thanks to Stefan Monnier for his guidance. --- doc/lispref/control.texi | 7 +++++++ etc/NEWS | 5 +++++ lisp/emacs-lisp/cl-macs.el | 8 ++++++++ test/lisp/emacs-lisp/pcase-tests.el | 10 ++++++++++ 4 files changed, 30 insertions(+) diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index 22b665b..efd9394 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 (cl-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{(cl-type integer)} +Example: @code{(cl-type (integer 0 10))} + @item (pred @var{function}) Matches if the predicate @var{function} returns non-@code{nil} when called on @var{expval}. The test can be negated with the syntax diff --git a/etc/NEWS b/etc/NEWS index 923cfcc..4505c6c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -515,6 +515,11 @@ in better code. --- *** New function 'pcase-compile-patterns' to write other macros. +*** 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))'. + +++ ** profiler.el The results displayed by 'profiler-report' now have the usage figures diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index cff4368..4cfc07a 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -3623,6 +3623,14 @@ cl-struct-slot-value "use `with-eval-after-load' instead." "28.1") (run-hooks 'cl-macs-load-hook) +;;; Pcase type pattern. + +;;;###autoload +(pcase-defmacro cl-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 2120139..02d3878 100644 --- a/test/lisp/emacs-lisp/pcase-tests.el +++ b/test/lisp/emacs-lisp/pcase-tests.el @@ -100,4 +100,14 @@ pcase-tests-or-vars (should (equal (funcall f 'b1) '(4 5 nil nil))) (should (equal (funcall f 'b2) '(nil nil 8 9))))) +(ert-deftest pcase-tests-cl-type () + (should (equal (pcase 1 + ((cl-type integer) 'integer)) + 'integer)) + (should (equal (pcase 1 + ((cl-type (integer 0 2)) 'integer-0<=n<=2)) + 'integer-0<=n<=2)) + (should-error (pcase 1 + ((cl-type notatype) 'integer)))) + ;;; pcase-tests.el ends here. -- 2.7.4 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-lisp-emacs-lisp-pcase.el-Add-type-pattern.patch Content-Description: "dumb" type pcase matcher >From b989a922fa712662cd24363ba4881ee42cd6add4 Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Sat, 10 Jul 2021 19:46:57 -0500 Subject: [PATCH 2/2] * lisp/emacs-lisp/pcase.el: Add type pattern * lisp/emacs-lisp/pcase.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. * etc/NEWS: Update news. With thanks to Stefan Monnier for his guidance. --- doc/lispref/control.texi | 8 ++++++++ etc/NEWS | 12 ++++++++---- lisp/emacs-lisp/pcase.el | 11 +++++++++++ test/lisp/emacs-lisp/pcase-tests.el | 7 +++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index efd9394..63acd1f 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi @@ -555,6 +555,14 @@ 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}, i.e. a type for which a +corresponding @code{typep} or @code{type-p} predicate is defined. For +matching structs defined with @code{cl-defstruct}, the @code{cl-type} +matcher should be used instead. + +Example: @code{(type integer)} + @item (cl-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}. diff --git a/etc/NEWS b/etc/NEWS index 4505c6c..b25e526 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -515,10 +515,14 @@ in better code. --- *** New function 'pcase-compile-patterns' to write other macros. -*** 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))'. +*** Added Pcase 'type' and 'cl-type' patterns. + +The new 'type' pattern compares simple types using the corresponding +'typep' or 'type-p' predicate, e.g. '(type integer)'. + +The new 'cl-type' pattern compares types using 'cl-typep', which +allows comparing simple types like '(cl-type integer)', as well as +forms like '(cl-type (integer 0 10))'. +++ ** profiler.el diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 006517d..c7e2027 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el @@ -1041,5 +1041,16 @@ let ;; (declare (debug (form))) ;; `(pred (lambda (_) ,expr))) +(pcase-defmacro type (type) + "Pcase pattern that matches objects of TYPE. +This checks using a predicate named `TYPEp' or `TYPE-p', as +appropriate. For matching structs defined with `cl-defstruct', +the `cl-type' pattern matcher should be used instead." + (let* ((type (symbol-name type)) + (pred (or (intern-soft (concat type "p")) + (intern-soft (concat type "-p")) + (error "Unknown type: %s" type)))) + `(pred ,pred))) + (provide 'pcase) ;;; pcase.el ends here diff --git a/test/lisp/emacs-lisp/pcase-tests.el b/test/lisp/emacs-lisp/pcase-tests.el index 02d3878..9184485 100644 --- a/test/lisp/emacs-lisp/pcase-tests.el +++ b/test/lisp/emacs-lisp/pcase-tests.el @@ -100,6 +100,13 @@ pcase-tests-or-vars (should (equal (funcall f 'b1) '(4 5 nil nil))) (should (equal (funcall f 'b2) '(nil nil 8 9))))) +(ert-deftest pcase-tests-type () + (should (equal (pcase 1 + ((type integer) 'integer)) + 'integer)) + (should-error (pcase 1 + ((type notatype) 'integer)))) + (ert-deftest pcase-tests-cl-type () (should (equal (pcase 1 ((cl-type integer) 'integer)) -- 2.7.4 --=-=-=--