Subject: bug#70597: Problem in pcase-let?

From: Stefan Monnier <[email-masked]>
Date: Sat, 04 May 2024 11:05:15 -0400

> sorry, but I do not think that the current doc string and/or the
> manual explains the situation.

Which "current" are you referring to?  The "current" manual (in
`master`) has a whole section on "Destructuring with pcase pattern"
(see below).

Do you still see room for misunderstanding there?
If so, can you suggest how to change it to make it more clear?

        Stefan

@node Destructuring with pcase Patterns
@subsection Destructuring with @code{pcase} Patterns
@cindex destructuring with pcase patterns

Pcase patterns not only express a condition on the form of the objects
they can match, but they can also extract sub-fields of those objects.
For example we can extract 2 elements from a list that is the value of
the variable @code{my-list} with the following code:

@example
  (pcase my-list
    (`(add ,x ,y)  (message "Contains %S and %S" x y)))
@end example

This will not only extract @code{x} and @code{y} but will additionally
test that @code{my-list} is a list containing exactly 3 elements and
whose first element is the symbol @code{add}.  If any of those tests
fail, @code{pcase} will immediately return @code{nil} without calling
@code{message}.

Extraction of multiple values stored in an object is known as
@dfn{destructuring}.  Using @code{pcase} patterns allows you to
perform @dfn{destructuring binding}, which is similar to a local
binding (@pxref{Local Variables}), but gives values to multiple
elements of a variable by extracting those values from an object of
compatible structure.

The macros described in this section use @code{pcase} patterns to
perform destructuring binding.  The condition of the object to be of
compatible structure means that the object must match the pattern,
because only then the object's subfields can be extracted.  For
example:

@example
  (pcase-let ((`(add ,x ,y) my-list))
    (message "Contains %S and %S" x y))
@end example

@noindent
does the same as the previous example, except that it directly tries
to extract @code{x} and @code{y} from @code{my-list} without first
verifying if @code{my-list} is a list which has the right number of
elements and has @code{add} as its first element.  The precise
behavior when the object does not actually match the pattern is
undefined, although the body will not be silently skipped: either an
error is signaled or the body is run with some of the variables
potentially bound to arbitrary values like @code{nil}.

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.

For an alternative facility for destructuring binding, see
@ref{seq-let}.

@defmac pcase-let bindings body@dots{}
Perform destructuring binding of variables according to
@var{bindings}, and then evaluate @var{body}.

@var{bindings} is a list of bindings of the form @w{@code{(@var{pattern}
@var{exp})}}, where @var{exp} is an expression to evaluate and
@var{pattern} is a @code{pcase} pattern.

All @var{exp}s are evaluated first, after which they are matched
against their respective @var{pattern}, introducing new variable
bindings that can then be used inside @var{body}.  The variable
bindings are produced by destructuring binding of elements of
@var{pattern} to the values of the corresponding elements of the
evaluated @var{exp}.

Here's a trivial example:

@example
(pcase-let ((`(,major ,minor)
	     (split-string "image/png" "/")))
  minor)
     @result{} "png"
@end example
@end defmac

@defmac pcase-let* bindings body@dots{}
Perform destructuring binding of variables according to
@var{bindings}, and then evaluate @var{body}.

@var{bindings} is a list of bindings of the form @code{(@var{pattern}
@var{exp})}, where @var{exp} is an expression to evaluate and
@var{pattern} is a @code{pcase} pattern.  The variable bindings are
produced by destructuring binding of elements of @var{pattern} to the
values of the corresponding elements of the evaluated @var{exp}.

Unlike @code{pcase-let}, but similarly to @code{let*}, each @var{exp}
is matched against its corresponding @var{pattern} before processing
the next element of @var{bindings}, so the variable bindings
introduced in each one of the @var{bindings} are available in the
@var{exp}s of the @var{bindings} that follow it, additionally to
being available in @var{body}.
@end defmac

@defmac pcase-dolist (pattern list) body@dots{}
Execute @var{body} once for each element of @var{list}, on each
iteration performing a destructuring binding of variables in
@var{pattern} to the values of the corresponding subfields of the
element of @var{list}.  The bindings are performed as if by
@code{pcase-let}.  When @var{pattern} is a simple variable, this ends
up being equivalent to @code{dolist} (@pxref{Iteration}).
@end defmac

@defmac pcase-setq pattern value@dots{}
Assign values to variables in a @code{setq} form, destructuring each
@var{value} according to its respective @var{pattern}.
@end defmac

@defmac pcase-lambda lambda-list &rest body
This is like @code{lambda}, but allows each argument to be a pattern.
For instance, here's a simple function that takes a cons cell as the
argument:

@example
(setq fun
      (pcase-lambda (`(,key . ,val))
        (vector key (* val 10))))
(funcall fun '(foo . 2))
    @result{} [foo 20]
@end example
@end defmac