GNU bug report logs - #67611
[PATCH] Add a Pcase pattern `cl-lambda` equivalent to `cl-destructuring-bind`

Previous Next

Package: emacs;

Reported by: Okamsn <okamsn <at> protonmail.com>

Date: Sun, 3 Dec 2023 20:35:01 UTC

Severity: wishlist

Tags: patch

Done: Stefan Kangas <stefankangas <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Okamsn <okamsn <at> protonmail.com>
To: João Távora <joaotavora <at> gmail.com>, Stefan Kangas <stefankangas <at> gmail.com>
Cc: 67611 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: bug#67611: [PATCH] Add a Pcase pattern `cl-lambda` equivalent to `cl-destructuring-bind`
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)))





This bug report was last modified 147 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.