unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#59887: pcase vs. pcase-let: Underscore in backquote-style patterns
@ 2022-12-07 16:28 hokomo
  2022-12-09  2:57 ` Michael Heerdegen
  2022-12-12  2:50 ` Michael Heerdegen
  0 siblings, 2 replies; 8+ messages in thread
From: hokomo @ 2022-12-07 16:28 UTC (permalink / raw)
  To: 59887


Hello,

How exactly is the underscore symbol treated in pcase's 
backquote-style patterns? Seems like at least pcase and pcase-let 
treat it inconsistently (I haven't checked the other pcase 
operators).

pcase treats the underscore as a literal symbol to match, hence 
this fails:

(pcase '(1 2 3)
  (`(1 _ ,x)
   x))

;; => nil

Adding the missing comma in front of the underscore gives us the 
expected behavior:

(pcase '(1 2 3)
  (`(1 ,_ ,x)
   x))

;; => 3

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

Additionally, I would think one would still be able to match a 
literal underscore symbol even with pcase-let, but the following 
still ends up matching:

(pcase-let ((`(1 ,'_ ,x) '(1 2 3)))
  x)

;; => 3

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. In other 
words, I would expect the following would be different from the 
above:

(pcase '(1 2 3)
  (`(1 _ ,x)
   x))

;; => 3 (instead of nil)

(pcase-let ((`(1 ,'_ ,x) '(1 2 3)))
  x)

;; => nil (instead of 3)

I'm not 100% sure if these requirements would cause any 
backwards-incompatible changes or inconsistencies with the other 
pcase operators though. I'm also assuming that `(1 _) and `(1 ,'_) 
can be distinguished, but maybe this is not true?

hokomo





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-12-13  2:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-07 16:28 bug#59887: pcase vs. pcase-let: Underscore in backquote-style patterns hokomo
2022-12-09  2:57 ` Michael Heerdegen
2022-12-12  2:50 ` Michael Heerdegen
2022-12-12 18:26   ` hokomo
2022-12-13  1:17     ` Michael Heerdegen
2022-12-13  1:19       ` hokomo
2022-12-13  2:21         ` Michael Heerdegen
2022-12-13  2:26           ` hokomo

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).