>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