>From 2aa7c4a6f651b482c916f7654935231425c3ce2a Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Mon, 9 Mar 2020 13:01:32 -0500 Subject: [PATCH] * 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. --- etc/NEWS | 7 +++++++ lisp/emacs-lisp/pcase.el | 8 ++++++++ test/lisp/emacs-lisp/pcase-tests.el | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index 47b87af..9190bf1 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -174,6 +174,13 @@ key binding / v package-menu-filter-by-version / / package-menu-filter-clear +** pcase.el + +*** Added Pcase 'type' pattern. + +A pattern like '(type integer)' is equivalent to one like +'(pred integerp)'. + * New Modes and Packages in Emacs 28.1 diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 36b93fa..334eb33 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el @@ -977,5 +977,13 @@ \` ;; compounded values that are not `consp' (t (error "Unknown QPAT: %S" qpat)))) +(pcase-defmacro type (type) + "Pcase pattern that matches objects of TYPE." + (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 0b69bd9..5db8605 100644 --- a/test/lisp/emacs-lisp/pcase-tests.el +++ b/test/lisp/emacs-lisp/pcase-tests.el @@ -71,6 +71,13 @@ 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-error (pcase 1 + ((type notatype) 'integer)))) + ;; Local Variables: ;; no-byte-compile: t ;; End: -- 2.7.4