emacs-devel
[Top][All Lists]
Advanced

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

Re: Replace trivial pcase occurrences in the Emacs sources


From: Michael Heerdegen
Subject: Re: Replace trivial pcase occurrences in the Emacs sources
Date: Sat, 03 Nov 2018 18:12:43 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Eli Zaretskii <address@hidden> writes:

> > | The pcase patterns that are useful for destructuring bindings are
> > | generally those described in @ref{Backquote Patterns}, since they
> > | express a specification of the structure of objects that will match.
> > 
> > I don't like that sentence for two reasons:
> > 
> > (1) ` is the pcase pattern for destructuring.  The "Backquote Patterns"
> > or "Backquote style Patterns" are not really "pcase patterns" since
> > they
> > are part of the implicit semantics ``' defines.  They are _not_ pcase
> > patterns.  Your sentence confuses the both.
>
> Sorry, you lost me here.  How can these not be pcase patterns, when
> they are used with 'pcase', and "pcase patterns" are _defined_ as
> those used with 'pcase'?

We called these UPATS in the past btw.

Not every part of a pattern is also a pattern.

,a is not an expression since it's only valid as sexp inside a backquote
expression.  For a similar reason, ,a is not a pcase pattern because
it's only valid in a ` pattern.

(< 1) is also not a pcase pattern, although it's valid inside a pred:
(pred (< 1)).

A pcase pattern should only be called what is valid as PATTERN in a
pcase clause (PATTERN BODY).

> > (2) We already have a lot other patterns for destructuring (eieio, seq,
> > map, cl-struct), and we probably will get even more in the future.
>
> OK, but why is that a reason not to use what I wrote?  Note that it
> talks about "destructuring binding", not just about destructuring.

Well, for binding there is only one pattern, SYMBOL.  But you also write

| since they express a specification of the structure of objects that
| will match.

so this seems to speak about destructuring and not about binding.

> Also please note that I made some changes in the description of
> seq-let to say that is an alternative facility for destructuring
> binding.
>
> The text you quoted doesn't say that those pcase patterns are the
> _only_ method of destructuring.  If that is the root cause of your
> disliking, we could make this even more clear.

I didn't speak about other methods in Emacs, I spoke about other pcase
patterns also performing destructuring.  But

| The pcase patterns that are useful for destructuring bindings are
| generally those described in @ref{Backquote Patterns}

makes it sounds if ` is the only method to get destructuring bindings
with pcase.


> > | @defmac pcase-let bindings address@hidden
> > | Perform desctructuring binding of variables according to
> > | @var{bindings}, and then evaluate @var{body}.
> > 
> > and lots of similar:
> > 
> > `pcase-let', `pcase-dolist' and the like are very often used only for
> > destructuring, but they are not limited to destructuring.  Like in
> > 
> > (defun test (arg)
> >   (pcase-let (((or (and (pred stringp) s)
> >                    (and (pred numberp) (app number-to-string s)))
> >                arg))
> >     s))
> > 
> > (test "Hallo") => "Hallo"
> > (test 1)       => "1"
>
> OK, but why would I need pcase-let in this example?  Isn't the above
> the same as
>
>   (defun test (arg)
>     (let ((s (cond ((stringp arg) arg)
>                    ((numberp arg) (number-to-string arg)))))
>       s))
>
> (which is both simpler and shorter)?

It was just an example.  And docstrings shouldn't tell restrictions that
don't exist just because you would prefer `cond' in such cases anyway.

> > Also the docstrings give the impression that these are limited to
> > destructuring, which is not true.
>
> Can you tell where did you get that impression?  The doc strings talk
> about destructuring bindings, when BINDINGS have the form specified,
> and I believe that is true.

You say

| Perform desctructuring binding of variables according to
| @var{bindings}, and then evaluate @var{body}.

Any pcase PATTERN can be used in BINDINGS, whether it performs
destructuring or not, the only assumption is that it should match.  You
can say that pcase-let is mostly useful for destructuring the bound
values, but as a summary of what `pcase-let' and the like is about it's
misleading.   It's misleading because I then wonder where the
restriction to destructuring comes from, and what's different to just
matching the patterns against the values as in `pcase'.


Michael.



reply via email to

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