Subject: bug#67611: [PATCH] Add a Pcase pattern `cl-lambda` equivalent to `cl-destructuring-bind`

From: Okamsn <[email-masked]>
Date: Fri, 12 Jan 2024 00:55:47 +0000

João Távora wrote:
> 4. Pcase integration is a "nice to have".  In the provided tests, the
>     new extension is only tested as a destructuring aid, so I cannot tell
>     very well how it fares int the matching aspect of pcase.  For example
>     I did this experiment:
> 
>     (pcase (list 42 41 :c 42)
>       ((cl-arglist (42 42 &key c))
>        (list c))
>       ((cl-arglist (42 b &rest more))
>        (list b more)))
> ;; => (42 2)
>     This matches the second case, which is good and returns (41 (:c 42))
> 
>     But how do I construct a case that matches only if the provided
>     value to c is 42, if this is at all possible?

It is possible. You can give a pattern instead of a symbol for the 
variable, but you have to use a version of `&key` variables in which the 
pattern can't be confused for the normal `((KEY VAR) DEFAULT SUPPLIED)` 
form.  Please try the following:

     ;; => (42 2)
     (pcase (list :c 42)
       ((cl-arglist (&key ((:c (and 43 c)))))
        (list c 1))
       ((cl-arglist (&key ((:c (and 42 c)))))
        (list c 2)))

     ;; => (42 2)
     (pcase (list :c 42)
           ((cl-arglist (&key ((:c (and 43 c)))))
            (list c 1))
           ((cl-arglist (&key ((:c (and (pred (equal 42))
                                        c)))))
            (list c 2))
           ((cl-arglist (&key ((:c (and 44 c)))))
            (list c 3)))