From 36e6a5880f2548c70a18e0d3266060ec5d49740f 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 f2f67a4..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 'cl-type' pattern. -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))'. +*** 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