emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pcase bindings in patterns with complicated logic


From: Richard Stallman
Subject: pcase bindings in patterns with complicated logic
Date: Fri, 12 Jan 2024 22:54:31 -0500

[[[ 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)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]