[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 44fe004: * lisp/emacs-lisp/cl-macs.el: Add cl-type pattern
From: |
Stefan Monnier |
Subject: |
master 44fe004: * lisp/emacs-lisp/cl-macs.el: Add cl-type pattern |
Date: |
Fri, 30 Jul 2021 17:23:37 -0400 (EDT) |
branch: master
commit 44fe0043d3671676867f302865b15bc3d90217b9
Author: Adam Porter <adam@alphapapa.net>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
* 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.
---
doc/lispref/control.texi | 10 ++++++++++
etc/NEWS | 5 +++++
lisp/emacs-lisp/cl-macs.el | 8 ++++++++
test/lisp/emacs-lisp/pcase-tests.el | 10 ++++++++++
4 files changed, 33 insertions(+)
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi
index 22b665b..5026d0a 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 36dc98d..ef115d0 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 cff4368..caf8bba 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 2120139..02d3878 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.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- master 44fe004: * lisp/emacs-lisp/cl-macs.el: Add cl-type pattern,
Stefan Monnier <=