Function: pcase-let
pcase-let is an autoloaded macro defined in pcase.el.gz.
Signature
(pcase-let BINDINGS &rest BODY)
Documentation
Like let, but supports destructuring BINDINGS using pcase patterns.
BODY should be a list of expressions, and BINDINGS should be a list of bindings of the form (PATTERN EXP). All EXPs are evaluated first, and then used to perform destructuring bindings by matching each EXP against its respective PATTERN. Then BODY is evaluated with those bindings in effect.
Each EXP should match its respective PATTERN (i.e. be of structure compatible to PATTERN); a mismatch may signal an error or may go undetected, binding variables to arbitrary values, such as nil.
Probably introduced at or before Emacs version 28.1.
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/pcase.el.gz
;;;###autoload
(defmacro pcase-let (bindings &rest body)
"Like `let', but supports destructuring BINDINGS using `pcase' patterns.
BODY should be a list of expressions, and BINDINGS should be a list of
bindings of the form (PATTERN EXP).
All EXPs are evaluated first, and then used to perform destructuring
bindings by matching each EXP against its respective PATTERN. Then
BODY is evaluated with those bindings in effect.
Each EXP should match its respective PATTERN (i.e. be of structure
compatible to PATTERN); a mismatch may signal an error or may go
undetected, binding variables to arbitrary values, such as nil."
(declare (indent 1) (debug pcase-let*))
(if (null (cdr bindings))
`(pcase-let* ,bindings ,@body)
(let ((matches '()))
(dolist (binding (prog1 bindings (setq bindings nil)))
(cond
((memq (car binding) pcase--dontcare-upats)
(push (cons (make-symbol "_") (cdr binding)) bindings))
((pcase--trivial-upat-p (car binding)) (push binding bindings))
(t
(let ((tmpvar (make-symbol (format "x%d" (length bindings)))))
(push (cons tmpvar (cdr binding)) bindings)
(push (list (car binding) tmpvar) matches)))))
`(let ,(nreverse bindings) (pcase-let* ,matches ,@body)))))