Subject: bug#47261: Destructuring with Pcase without assigning values

From: Stefan Monnier <[email-masked]>
Date: Fri, 19 Mar 2021 17:45:00 -0400

> For a Pcase pattern, it would be convenient to have Pcase destructure
> the pattern and have a way to see what values Pcase would like to assign
> to variables within the pattern.
>
> This would allow one to know the variables found in the pattern and to
> manipulate the values that Pcase would like to assign, instead of just
> letting `pcase` assign them.

I massaged the code so that it now exports the following function:

    (defun pcase-compile-patterns (exp cases)
      "Compile the set of patterns in CASES.
    EXP is the expression that will be matched against the patterns.
    CASES is a list of elements (PAT . CODEGEN)
    where CODEGEN is a function that returns the code to use when
    PAT matches.  That code has to be in the form of a cons cell.
    
    CODEGEN will be called with at least 2 arguments, VARVALS and COUNT.
    VARVALS is a list of elements of the form (VAR VAL . RESERVED) where VAR
    is a variable bound by the pattern and VAL is a duplicable expression
    that returns the value this variable should be bound to.
    If the pattern PAT uses `or', CODEGEN may be called multiple times,
    in which case it may want to generate the code differently to avoid
    a potential code explosion.  For this reason the COUNT argument indicates
    how many time this CODEGEN is called."

The `pcase` macro itself uses this function, so it should hopefully be
"good enough", but let me know if it's causing you trouble.

        Stefan