GNU bug report logs - #59887
pcase vs. pcase-let: Underscore in backquote-style patterns

Previous Next

Package: emacs;

Reported by: hokomo <hokomo <at> airmail.cc>

Date: Wed, 7 Dec 2022 17:10:02 UTC

Severity: normal

Done: Michael Heerdegen <michael_heerdegen <at> web.de>

Bug is archived. No further changes may be made.

Full log


Message #8 received at 59887 <at> debbugs.gnu.org (full text, mbox):

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: hokomo <hokomo <at> airmail.cc>
Cc: 59887 <at> debbugs.gnu.org
Subject: Re: bug#59887: pcase vs. pcase-let: Underscore in backquote-style
 patterns
Date: Fri, 09 Dec 2022 03:57:31 +0100
hokomo <hokomo <at> airmail.cc> writes:

> However, pcase-let is less strict about this, producing the same
> result with or without the comma:
>
> (pcase-let ((`(1 _ ,x) '(1 2 3)))
>  x)
>
> ;; => 3
>
> (pcase-let ((`(1 ,_ ,x) '(1 2 3)))
>  x)
>
> ;; => 3

Note this part of the `pcase-let' documentation string:

| Each EXP should match (i.e. be of compatible structure) to its
| respective PATTERN; a mismatch may signal an error or may go
| undetected, binding variables to arbitrary values, such as nil.

Your first case is invalid because the pattern doesn't match the value.
Here it goes undetected and bindings get established.

This behavior is not perfect, but AFAIR it has been preferred over the
less efficient code that better checks would mean.  So it's the
programmer's task to use only matching patterns.  This is not really a
restriction because `pcase-let' is intended to create bindings, not for
testing whether a pattern matches some value.


> I think that matching a literal underscore symbol is rare enough that
> the ideal behavior would probably be for an underscore within a
> backquote template to be treated as a wildcard whenever it appears
> literally (e.g., `(1 _)) or unquoted (e.g., `(1 ,_)). However, as soon
> as explicitly quoted (e.g., `(1 ,'_)), it should be treated as a match
> for a literal underscore symbol.

This idea had been discussed in the past.  It had some votes but it had
been decided not to implement such a feature because it would not really
fit into the existing semantics, just for the sake of leaving out one
",".  So I'm afraid I don't think we will change this.


Michael.




This bug report was last modified 2 years and 240 days ago.

Previous Next


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