From d8dd59279a6035bb4fee515ded74efa7824cdeca Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sat, 15 Mar 2025 10:44:26 +0100 Subject: compat-28: New pcase pattern cl-type --- NEWS.org | 1 + compat-28.el | 8 ++++++++ compat-tests.el | 8 ++++++++ compat.texi | 4 ++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/NEWS.org b/NEWS.org index 15ee4f6..48b20ce 100644 --- a/NEWS.org +++ b/NEWS.org @@ -4,6 +4,7 @@ * Development +- compat-28: New pcase pattern =cl-type=. - compat-31: New macros =static-when= and =static-unless=. - compat-31: New functions =oddp= and =evenp=. - compat-31: New functions =minusp= and =plusp=. diff --git a/compat-28.el b/compat-28.el index 9834044..ae4978d 100644 --- a/compat-28.el +++ b/compat-28.el @@ -849,5 +849,13 @@ function will never return nil." :type-error "This field should contain a nonnegative integer" :match-alternatives '(natnump))) +;;;; Defined in pcase.el + +(compat-guard t ;; + (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 (lambda (x) (cl-typep x ',type))))) + (provide 'compat-28) ;;; compat-28.el ends here diff --git a/compat-tests.el b/compat-tests.el index eeb889b..3ea386a 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -2921,6 +2921,14 @@ (cl-with-gensyms (x y) `(let ((,x 1) (,y 2)) (+ ,x ,y)))) +(ert-deftest compat-pcase-cl-type () + (should-equal "int" (pcase 1 ((cl-type fixnum) "int"))) + (should-equal "int" (pcase 1 ((cl-type integer) "int"))) + (should-equal "int" (pcase 1 ((cl-type (integer 0 10)) "int"))) + (should-equal "bool" (pcase t ((cl-type boolean) "bool"))) + (should-equal "bool" (pcase nil ((cl-type boolean) "bool"))) + (should-not (pcase t ((cl-type fixnum) "fixnum")))) + (ert-deftest compat-cl-with-gensyms () (should-equal 3 (compat-tests--with-gensyms))) diff --git a/compat.texi b/compat.texi index a54aa93..222b799 100644 --- a/compat.texi +++ b/compat.texi @@ -1408,8 +1408,8 @@ The following functions and macros are implemented in Emacs 28.1. These functions are made available by Compat on Emacs versions older than 28.1. -The @code{defcustom} type @code{natnum} introduced in Emacs 28.1 is -made available by Compat. +The @code{defcustom} type @code{natnum} and the @code{pcase} pattern +@code{cl-type} introduced in Emacs 28.1 are made available by Compat. @c copied from lispref/processes.texi @defun process-lines-ignore-status program &rest args -- cgit v1.0