help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: How to extract bindings from `pcase-let*`?


From: Okam
Subject: Re: How to extract bindings from `pcase-let*`?
Date: Tue, 16 Mar 2021 01:50:32 +0000

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.




reply via email to

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