Subject: Re: bug#59887: pcase vs. pcase-let: Underscore in backquote-style patterns

From: Michael Heerdegen <[email-masked]>
Date: Fri, 09 Dec 2022 03:57:31 +0100

hokomo <[email-masked]> 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.