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: Mon, 09 Mar 2020 14:35:35 -0500 Message-ID: <87mu8pwcc8.fsf@alphapapa.net> References: <874kuxxuez.fsf@alphapapa.net> <837dztbcgu.fsf@gnu.org> <87v9ndwd1m.fsf@alphapapa.net> 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="57493"; 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 Mon Mar 09 20:40:39 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 1jBOGT-000Eqe-Vm for ged-emacs-devel@m.gmane-mx.org; Mon, 09 Mar 2020 20:40:37 +0100 Original-Received: from localhost ([::1]:48702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBOGS-0002Kh-Vv for ged-emacs-devel@m.gmane-mx.org; Mon, 09 Mar 2020 15:40:37 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53077) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBOFw-0001nF-DJ for emacs-devel@gnu.org; Mon, 09 Mar 2020 15:40:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jBOFv-000317-8b for emacs-devel@gnu.org; Mon, 09 Mar 2020 15:40:04 -0400 Original-Received: from ciao.gmane.io ([159.69.161.202]:33696) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jBOFv-00030T-15 for emacs-devel@gnu.org; Mon, 09 Mar 2020 15:40:03 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1jBOFt-000ECa-No for emacs-devel@gnu.org; Mon, 09 Mar 2020 20:40:01 +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:245406 Archived-At: --=-=-= Content-Type: text/plain Adam Porter writes: > Eli Zaretskii writes: > >> Thanks for working on this. I'll let others comment on whether this >> should be added, but if it is, it should be documented in the ELisp >> manual, like all the other patterns supported by pcase. > > Thanks, I'll prepare another patch that updates the manual and pcase's > docstring. This patch also updates the Elisp manual and the pcase docstring accordingly. It does not yet take into account Stefan's feedback, as I'm awaiting further instructions. Thanks. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-lisp-emacs-lisp-pcase.el-Add-type-pattern.patch Content-Description: Add pcase type pattern (also updates docs) >From 51a96e7f53c92d1d9d893054b73b0a0c2577dfbf Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Mon, 9 Mar 2020 13:01:32 -0500 Subject: [PATCH] * lisp/emacs-lisp/pcase.el: Add type pattern * lisp/emacs-lisp/pcase.el: ((pcase-defmacro type)): Add 'type' pattern. (pcase): Update docstring. * test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-type): Add test. * doc/lispref/control.texi (pcase Macro): Update manual. --- doc/lispref/control.texi | 7 +++++++ etc/NEWS | 7 +++++++ lisp/emacs-lisp/pcase.el | 9 +++++++++ test/lisp/emacs-lisp/pcase-tests.el | 7 +++++++ 4 files changed, 30 insertions(+) diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index c601e3a..848d6a3 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}. The comparison is done +with a function named @var{typep} or @var{type-p}, whichever is +defined. + +Example: @code{(type integer)} + @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..9190bf1 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -174,6 +174,13 @@ key binding / v package-menu-filter-by-version / / package-menu-filter-clear +** pcase.el + +*** Added Pcase 'type' pattern. + +A pattern like '(type integer)' is equivalent to one like +'(pred integerp)'. + * New Modes and Packages in Emacs 28.1 diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 36b93fa..628e961 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el @@ -128,6 +128,7 @@ pcase If a SYMBOL is used twice in the same pattern the second occurrence becomes an `eq'uality test. (pred FUN) matches if FUN called on EXPVAL returns non-nil. + (type TYPE) matches if EXPVAL is of TYPE. (app FUN PAT) matches if FUN called on EXPVAL matches PAT. (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. (let PAT EXPR) matches if EXPR matches PAT. @@ -977,5 +978,13 @@ \` ;; compounded values that are not `consp' (t (error "Unknown QPAT: %S" qpat)))) +(pcase-defmacro type (type) + "Pcase pattern that matches objects of TYPE." + (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 0b69bd9..5db8605 100644 --- a/test/lisp/emacs-lisp/pcase-tests.el +++ b/test/lisp/emacs-lisp/pcase-tests.el @@ -71,6 +71,13 @@ 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-error (pcase 1 + ((type notatype) 'integer)))) + ;; Local Variables: ;; no-byte-compile: t ;; End: -- 2.7.4 --=-=-=--