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 10:46:24 -0500 Message-ID: <87wo7s8b73.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> 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="66439"; 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 16:47:32 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 1jBh6S-000H9B-DH for ged-emacs-devel@m.gmane-mx.org; Tue, 10 Mar 2020 16:47:32 +0100 Original-Received: from localhost ([::1]:35968 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBh6R-00068k-CG for ged-emacs-devel@m.gmane-mx.org; Tue, 10 Mar 2020 11:47:31 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48225) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBh5V-0005cd-Nt for emacs-devel@gnu.org; Tue, 10 Mar 2020 11:46:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jBh5U-0001jR-FL for emacs-devel@gnu.org; Tue, 10 Mar 2020 11:46:33 -0400 Original-Received: from ciao.gmane.io ([159.69.161.202]:60608) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jBh5U-0001f2-7X for emacs-devel@gnu.org; Tue, 10 Mar 2020 11:46:32 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1jBh5S-000FfI-ND for emacs-devel@gnu.org; Tue, 10 Mar 2020 16:46:30 +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:245429 Archived-At: --=-=-= Content-Type: text/plain Michael Heerdegen writes: >> +(pcase-defmacro type (type) >> + "Pcase pattern that matches objects of TYPE." >> + `(pred (pcase--flip cl-typep ',type))) > > If we keep these semantics and the patch is accepted, you should > document here what TYPE means. IIUC any type expression allowed by > `cl-typep' will be possible, right? Then just say something like "TYPE > is a type name like in `cl-typep'" or so. 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. --=-=-= 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 e588e09a708e23e3d6e5b140c8fde4b0aabd60c3 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 | 7 +++++++ test/lisp/emacs-lisp/pcase-tests.el | 10 ++++++++++ 4 files changed, 32 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..dc272e3 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -3403,6 +3403,13 @@ cl-struct-slot-value (run-hooks 'cl-macs-load-hook) +;;; Pcase type pattern. + +(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 --=-=-=--