all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* pcase bindings in patterns with complicated logic
@ 2024-01-13  3:54 Richard Stallman
  2024-01-13 19:58 ` Ihor Radchenko
  2024-01-14  7:03 ` Thierry Volpiatto
  0 siblings, 2 replies; 21+ messages in thread
From: Richard Stallman @ 2024-01-13  3:54 UTC (permalink / raw)
  To: emacs-devel

[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

I'm trying to figure out how pcase behaves in terms of which pattern
variables are actually bound, depending on which parts of a case's
pattern succeeds in matching.  That wasn't clear to me from the docs I
read.

I tried this example:

(setq x1 nil x2 nil y1 nil y2 nil)

(pcase '(foo 5)
  ((or `(,(and (pred symbolp) x1) ,(and (pred symbolp) y1))
       `(,(and (pred symbolp) x2) ,(and (pred numberp) y2)))
   `(,x1 ,y1 ,x2 ,y2)))

=> (nil nil foo 5)

So it seems that unless a match for a variable is included in the
branch of the matching tree that actually completes, its variable is
not set.

But then I tried

(pcase '(foo 5)
  ((or `(,(and (pred symbolp) a1) ,(and (pred symbolp) b1))
       `(,(and (pred symbolp) a2) ,(and (pred numberp) b2)))
   `(,a1 ,b1 ,a2 ,b2)))

=> (nil nil foo 5)

That surrised me, since outside the pcase, all four variables
are unbound and referring to them gets errors.

So it seems that all the pattern variables in the curren clause are
bound somehow if that clause succeeds, but only some of them get
significant values.

Is that an accurate general statement of how pcase handles binding
pattern variables?

If so, do people like that behavior?  Is it easy to use?  Would anyone
prefer a different behavior for cases like this?

The question is, how should cond* handle this?  Just like pcase, or
something different?


-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





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

end of thread, other threads:[~2024-01-23 19:59 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-13  3:54 pcase bindings in patterns with complicated logic Richard Stallman
2024-01-13 19:58 ` Ihor Radchenko
2024-01-14  6:58   ` Thierry Volpiatto
2024-01-14  7:27     ` Thierry Volpiatto
2024-01-14 15:42     ` Ihor Radchenko
2024-01-14 15:58       ` Thierry Volpiatto
2024-01-14 16:21         ` Ihor Radchenko
2024-01-14 17:54           ` Thierry Volpiatto
2024-01-16  3:32             ` Richard Stallman
2024-01-16 13:18               ` Ihor Radchenko
2024-01-18  3:37                 ` Richard Stallman
2024-01-18 13:13                   ` Ihor Radchenko
2024-01-20  3:39                     ` Richard Stallman
2024-01-20 12:48                       ` Ihor Radchenko
2024-01-21  3:06                         ` Richard Stallman
2024-01-21 14:06                           ` Ihor Radchenko
2024-01-23 13:40                             ` Richard Stallman
2024-01-23 15:08                             ` Stefan Monnier via Emacs development discussions.
2024-01-23 19:59                               ` Stefan Monnier via Emacs development discussions.
2024-01-15  3:13   ` Richard Stallman
2024-01-14  7:03 ` Thierry Volpiatto

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.