From: Adam Porter Date: Mon, 9 Mar 2020 18:01:32 +0000 (-0500) Subject: * lisp/emacs-lisp/cl-macs.el: Add cl-type pattern X-Git-Tag: emacs-28.0.90~1639 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=44fe0043d3671676867f302865b15bc3d90217b9;p=emacs.git * 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 and Eli Zaretskii for their guidance. --- diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index 22b665bc931..5026d0a4d70 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi @@ -555,6 +555,16 @@ Two symbols to avoid are @code{t}, which behaves like @code{_} 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 type +descriptor as accepted by @code{cl-typep} (@pxref{cl-typep,,,cl,Common +Lisp Extensions}). Examples: + +@lisp +(cl-type integer) +(cl-type (integer 0 10)) +@end lisp + @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 36dc98d8c09..ef115d0ae01 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -545,6 +545,11 @@ in better code. --- *** New function 'pcase-compile-patterns' to write other macros. +*** Added '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))'. + +++ ** 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 cff43689405..caf8bba2f8c 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -3623,6 +3623,14 @@ STRUCT and SLOT-NAME are symbols. INST is a structure instance." "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 type descriptor 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 2120139ec18..02d3878ad08 100644 --- a/test/lisp/emacs-lisp/pcase-tests.el +++ b/test/lisp/emacs-lisp/pcase-tests.el @@ -100,4 +100,14 @@ (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.