From: Okam <okamsn@protonmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to extract bindings from `pcase-let*`?
Date: Tue, 16 Mar 2021 01:50:32 +0000 [thread overview]
Message-ID: <3217e2b9-1b58-1984-ba0a-4526e76cfb4d@protonmail.com> (raw)
In-Reply-To: <jwvwnu8v370.fsf-monnier+emacs@gnu.org>
On 3/15/21 11:38 AM, Stefan Monnier wrote:
>
>> I would like to extract the bindings created by `pcase-let*` to use them
>> in a `setq` form. Is there a good way of doing this?
>
> Sending me a patch to `pcase.el` which does that?
OK. I wanted to see if there was an intentional entry point first.
>> Previously, I was using `macroexpand` on `pcase-let*` and extracting the
>> bindings from the list, but after the expansion produced by `pcase-let*`
>> changed recently, I realized that this way assumes too much.
>
> Interesting. Can you show me the code you used?
This is the simple code I used:
(defun loopy-pcase--get-variable-values (var val)
"Destructure VAL according to VAR using `pcase'.
Return a list of 2 sub-lists: (1) the needed generated variables
and (2) the variables actually named in VAR.
VAR should be a normal `pcase' destructuring pattern, such as
\"`(a . ,b)\" or \"`(1 2 3 . ,rest)\"."
;; Using `pcase-let*' as an interface, since it is a public function.
;; `pcase' knows to not assign variables if they are unused, so
we pass
;; back in `var' (a backquoted list) so that it thinks the variables
;; are used.
;;
;; This will give a form like
;; (let* (temp-vars) (let (actual-vars) VAR))
;;
;; NOTE: Named variables might be in reverse order. Not sure if
this is
;; reliable behavior.
(pcase-let* ((`(let* ,temp-vars (let ,true-vars . ,_))
(macroexpand `(pcase-let* ((,var ,val)) ,var))))
(list temp-vars true-vars)))
It assumes too much, and is not resilient to changes in how `pcase-let*`
expands. I don't fully understand the code of Pcase yet, and failed to
find something like Dash's `dash--match`, which returns a list of
bindings that can be substituted into forms like `setq` or `let*`.
I am doing this to attempt to optionally use Pcase for destructuring in
a macro that I am writing.
next prev parent reply other threads:[~2021-03-16 1:50 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-15 1:43 How to extract bindings from `pcase-let*`? Okam
2021-03-15 15:38 ` Stefan Monnier
2021-03-16 1:50 ` Okam [this message]
2021-03-16 2:45 ` Stefan Monnier
2021-03-17 1:33 ` Okam
2021-03-17 2:59 ` 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=3217e2b9-1b58-1984-ba0a-4526e76cfb4d@protonmail.com \
--to=okamsn@protonmail.com \
--cc=help-gnu-emacs@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.