[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to extract bindings from `pcase-let*`?
From: |
Stefan Monnier |
Subject: |
Re: How to extract bindings from `pcase-let*`? |
Date: |
Mon, 15 Mar 2021 22:45:32 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
> (pcase-let* ((`(let* ,temp-vars (let ,true-vars . ,_))
> (macroexpand `(pcase-let* ((,var ,val)) ,var))))
> (list temp-vars true-vars)))
Indeed it's not reliable: for a case like
(pcase-let (((or `(inorder ,x ,y) `(reverse ,y ,x)) FOO))
(cons x y))
it will generate code comparable to
(if (eq (car FOO) 'inorder)
(cons (nth 1 FOO) (nth 2 FOO))
(cons (nth 2 FOO) (nth 1 FOO))
[ Just obfuscated with gensym'd vars and such ]
And if you replace the trivial (cons x y) with something less trivial it
will turn into something comparable to:
(let ((body (lambda (x y) <something-less-trivial>)))
(if (eq (car FOO) 'inorder)
(funcall body (nth 1 FOO) (nth 2 FOO))
(funcall body (nth 2 FOO) (nth 1 FOO))))
so it's far from clear how to represent this kind of code in the form of
"a list of bindings that can be substituted into forms like `setq` or
`let*`"
OTOH, I think `pcase.el` could offer a function which could take
a "buildbody" function as argument, where that buildbody would receive
something like the list (x y) and would be expected to return the
(lambda (x y) <something-less-trivial>) to use. So you could pass it a
"buildbody"
function which takes the list (x y) and returns something like
(lambda (tmp1 tmp2) (setq x tmp1 y tmp2)), which I expect would do more
or less what you want.
> I am doing this to attempt to optionally use Pcase for destructuring in
> a macro that I am writing.
`pcase-let` already does destructuring, so could you explain in a bit
more detail what kind of destructuring you're looking for (and why)?
Stefan