all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Adam Porter <adam@alphapapa.net>
To: emacs-devel@gnu.org
Subject: [PATCH] pcase.el: Add type pattern
Date: Mon, 09 Mar 2020 13:19:48 -0500	[thread overview]
Message-ID: <874kuxxuez.fsf@alphapapa.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 378 bytes --]

Hi,

It seemed like an obvious and useful improvement to add a "type" pattern
to pcase, so I prepared this patch.  I wondered if checking for
predicates like this was a good way to do so, and I saw that the
cl-typep inliner does basically this, so it seems reasonable.

If this seems like an acceptable idea, please let me know if any further
changes are required.

Thanks,
Adam

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Add pcase type pattern --]
[-- Type: text/x-diff, Size: 2144 bytes --]

From 15248b62ada8e7d5c5484a0dd4749e057b0b8061 Mon Sep 17 00:00:00 2001
From: Adam Porter <adam@alphapapa.net>
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)'.
+
 \f
 * New Modes and Packages in Emacs 28.1
 
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index 36b93fa..4a2903b 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


             reply	other threads:[~2020-03-09 18:19 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-09 18:19 Adam Porter [this message]
2020-03-09 18:38 ` [PATCH] pcase.el: Add type pattern Eli Zaretskii
2020-03-09 19:20   ` Adam Porter
2020-03-09 19:35     ` Adam Porter
2020-03-09 18:38 ` Adam Porter
2020-03-09 18:45 ` Eric Abrahamsen
2020-03-10 15:48   ` Adam Porter
2020-03-10 17:40     ` Eric Abrahamsen
2020-03-09 18:53 ` Stefan Monnier
2020-03-09 19:31   ` Adam Porter
2020-03-09 21:00     ` Stefan Monnier
2020-03-09 21:54       ` Adam Porter
2020-03-09 22:22         ` Michael Heerdegen
2020-03-09 22:46           ` Adam Porter
2020-03-10 14:54             ` Michael Heerdegen
2020-03-10 15:01             ` Michael Heerdegen
2020-03-10 15:46               ` Adam Porter
2020-03-10 16:24                 ` Michael Heerdegen
2020-03-10 17:03                   ` Adam Porter
2020-03-10 17:43                     ` Michael Heerdegen
2020-03-10 18:04                       ` Adam Porter
2020-03-10 18:17                         ` Adam Porter
2020-03-10 19:07                           ` Michael Heerdegen
2020-03-10 19:19                             ` Adam Porter
2020-03-10 19:10                           ` Eric Abrahamsen
2020-03-10 19:21                             ` Adam Porter
2020-03-11  2:08                         ` Michael Heerdegen
2020-03-16 12:59                 ` Stefan Monnier
2021-07-11  1:33                   ` [PATCH] pcase.el: Add cl-type and type patterns Adam Porter
2021-07-11  2:12                     ` Adam Porter
2021-07-11  6:11                       ` Eli Zaretskii
2021-07-16  9:32                         ` Adam Porter
2021-07-17 11:58                           ` Eli Zaretskii
2021-07-16 22:41                       ` Stefan Monnier
2021-07-17 16:07                         ` Adam Porter
2021-07-30 21:24                           ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874kuxxuez.fsf@alphapapa.net \
    --to=adam@alphapapa.net \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.