emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cond*


From: Adam Porter
Subject: Re: cond*
Date: Sun, 17 Dec 2023 23:41:22 -0600
User-agent: Mozilla Thunderbird

To finish the design of cond*, we need to find what features
needed to be added so that it is rare to encounter a pcase
that can't be replaced cleanly.    Would people please tell me
about possible examples?

Well, one of the most common things I do with pcase is to destructure alists, plists, and structs, often nested at multiple levels. For example:

(defun ement-ignore-user (user-id session &optional unignore-p)
  "Ignore USER-ID on SESSION.
If UNIGNORE-P (interactively, with prefix), un-ignore USER."
  (interactive (list (ement-complete-user-id)
                     (ement-complete-session)
                     current-prefix-arg))
  ;; NOTE: Destructures account-data slot of ement-session struct.
  (pcase-let* (((cl-struct ement-session account-data) session)
               ;; NOTE: Destructures alist nested in another alist and
               ;; binds to a variable of a different name than the key.
               ((map ('content (map ('ignored_users ignored-users))))
                (cl-find "m.ignored_user_list" account-data
:key (lambda (event) (alist-get 'type event)) :test #'equal)))
    ...))

(defun ement--room-in-space-p (room space)
  "Return non-nil if ROOM is in SPACE on SESSION."
  ;; NOTE: Destructures slots in two structs to alternative variable
  ;; names, and destructures an alist key in each struct's `local'
  ;; slot to a variable of the same name.
(pcase-let* (((cl-struct ement-room (id parent-id) (local (map children))) space) ((cl-struct ement-room (id child-id) (local (map parents))) room))
    ...))

No alternative to pcase that omitted these features could take its place for me. In a large package like Ement.el, where function after function destructures similar objects in similar ways, the benefit of the consistency, conciseness, and flexibility offered by pcase can't be understated.

As well, pcase is easily extended with its built-in flexibility, e.g. how easily support was added for the cl-type based pattern. It's a very well-designed system.

Then there are pcase-lambda, pcase-dolist, and pcase-setq, all of which use the same underlying facilities and patterns. It's a coherent system: learning pcase opens up this world of useful forms. I would not want to give up these powerful tools which make my programs better and my programming more enjoyable.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]