aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2018-11-03 15:11:33 +0200
committerEli Zaretskii2018-11-03 15:11:33 +0200
commite824c914dabd92537a0d6e44eaa10bb4699c312f (patch)
treec867f703cbf7b34400201b39af1fcf69a23ee1c0
parent74bc0e16b7f9fdc5011c28182a2c8d828ee426d8 (diff)
downloademacs-e824c914dabd92537a0d6e44eaa10bb4699c312f.tar.gz
emacs-e824c914dabd92537a0d6e44eaa10bb4699c312f.zip
Improve documentation of destructuring-binding macros
* lisp/emacs-lisp/pcase.el (pcase-dolist, pcase-let) (pcase-let*): Improve the doc strings. * doc/lispref/sequences.texi (Sequence Functions): Improve wording and rename arguments of seq-let to be more descriptive. Add a cross-reference to "Destructuring with pcase Patterns". * doc/lispref/control.texi (Pattern-Matching Conditional): Improve wording and the menu. (pcase Macro): Incorporate patch suggested by Paul Eggert <eggert@cs.ucla.edu>. Reformat text. (Destructuring with pcase Patterns): Rename from "Destructuring patterns", and improve wording and indexing.
-rw-r--r--doc/lispref/control.texi228
-rw-r--r--doc/lispref/sequences.texi20
-rw-r--r--lisp/emacs-lisp/pcase.el43
3 files changed, 160 insertions, 131 deletions
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi
index 06c6622bf01..f80622e6025 100644
--- a/doc/lispref/control.texi
+++ b/doc/lispref/control.texi
@@ -419,65 +419,68 @@ This is not completely equivalent because it can evaluate @var{arg1} or
419@node Pattern-Matching Conditional 419@node Pattern-Matching Conditional
420@section Pattern-Matching Conditional 420@section Pattern-Matching Conditional
421@cindex pcase 421@cindex pcase
422@cindex pattern matching 422@cindex pattern matching, programming style
423 423
424Aside from the four basic conditional forms, Emacs Lisp also 424Aside from the four basic conditional forms, Emacs Lisp also
425has a pattern-matching conditional form, the @code{pcase} macro, 425has a pattern-matching conditional form, the @code{pcase} macro,
426a hybrid of @code{cond} and @code{cl-case} 426a hybrid of @code{cond} and @code{cl-case}
427(@pxref{Conditionals,,,cl,Common Lisp Extensions}) 427(@pxref{Conditionals,,,cl,Common Lisp Extensions})
428that overcomes their limitations and introduces 428that overcomes their limitations and introduces
429the @dfn{pattern matching} programming style. 429the @dfn{pattern matching programming style}.
430First, the limitations: 430The limitations that @code{pcase} overcomes are:
431 431
432@itemize 432@itemize
433@item The @code{cond} form chooses among alternatives 433@item
434by evaluating the predicate @var{condition} of each 434The @code{cond} form chooses among alternatives by evaluating the
435of its clauses (@pxref{Conditionals}). 435predicate @var{condition} of each of its clauses
436The primary limitation is that variables let-bound in @var{condition} 436(@pxref{Conditionals}). The primary limitation is that variables
437are not available to the clause's @var{body-forms}. 437let-bound in @var{condition} are not available to the clause's
438@var{body-forms}.
438 439
439Another annoyance (more an inconvenience than a limitation) 440Another annoyance (more an inconvenience than a limitation)
440is that when a series of @var{condition} predicates implement 441is that when a series of @var{condition} predicates implement
441equality tests, there is a lot of repeated code. 442equality tests, there is a lot of repeated code. (@code{cl-case}
442For that, why not use @code{cl-case}? 443solves this inconvenience.)
443 444
444@item 445@item
445The @code{cl-case} macro chooses among alternatives by evaluating 446The @code{cl-case} macro chooses among alternatives by evaluating
446the equality of its first argument against a set of specific 447the equality of its first argument against a set of specific
447values. 448values.
448The limitations are two-fold: 449
450Its limitations are two-fold:
449 451
450@enumerate 452@enumerate
451@item The equality tests use @code{eql}. 453@item
452@item The values must be known and written in advance. 454The equality tests use @code{eql}.
455@item
456The values must be known and written in advance.
453@end enumerate 457@end enumerate
454 458
455@noindent 459@noindent
456These render @code{cl-case} unsuitable for strings or compound 460These render @code{cl-case} unsuitable for strings or compound
457data structures (e.g., lists or vectors). 461data structures (e.g., lists or vectors). (@code{cond} doesn't have
458For that, why not use @code{cond}? 462these limitations, but it has others, see above.)
459(And here we end up in a circle.)
460@end itemize 463@end itemize
461 464
462@noindent 465@noindent
463Conceptually, the @code{pcase} macro borrows the first-arg focus 466Conceptually, the @code{pcase} macro borrows the first-arg focus
464of @code{cl-case} and the clause-processing flow of @code{cond}, 467of @code{cl-case} and the clause-processing flow of @code{cond},
465replacing @var{condition} with a generalization of 468replacing @var{condition} with a generalization of
466the equality test called @dfn{matching}, 469the equality test which is a variant of @dfn{pattern matching},
467and adding facilities so that you can concisely express a 470and adding facilities so that you can concisely express a
468clause's predicate, and arrange to share let-bindings between 471clause's predicate, and arrange to share let-bindings between
469a clause's predicate and @var{body-forms}. 472a clause's predicate and @var{body-forms}.
470 473
471The concise expression of a predicate is known as a @dfn{pattern}. 474The concise expression of a predicate is known as a @dfn{pattern}.
472When the predicate, called on the value of the first arg, 475When the predicate, called on the value of the first arg, returns
473returns non-@code{nil}, the pattern matches the value 476non-@code{nil}, we say that ``the pattern matches the value'' (or
474(or sometimes ``the value matches the pattern''). 477sometimes ``the value matches the pattern'').
475 478
476@menu 479@menu
477* The @code{pcase} macro: pcase Macro. Plus examples and caveats. 480* The @code{pcase} macro: pcase Macro. Includes examples and caveats.
478* Extending @code{pcase}: Extending pcase. Define new kinds of patterns. 481* Extending @code{pcase}: Extending pcase. Define new kinds of patterns.
479* Backquote-Style Patterns: Backquote Patterns. Structural matching. 482* Backquote-Style Patterns: Backquote Patterns. Structural patterns matching.
480* Destructuring patterns:: Using pcase patterns to extract subfields. 483* Destructuring with pcase Patterns:: Using pcase patterns to extract subfields.
481@end menu 484@end menu
482 485
483@node pcase Macro 486@node pcase Macro
@@ -498,30 +501,30 @@ of the last of @var{body-forms} in the successful clause.
498Otherwise, @code{pcase} evaluates to @code{nil}. 501Otherwise, @code{pcase} evaluates to @code{nil}.
499@end defmac 502@end defmac
500 503
501Each @var{pattern} has to be a @dfn{pcase pattern}, which can either 504@cindex pcase pattern
502use one of the core patterns defined below, or use one of the patterns 505Each @var{pattern} has to be a @dfn{pcase pattern}, which can use
503defined via @code{pcase-defmacro}. 506either one of the core patterns defined below, or one of the patterns
507defined via @code{pcase-defmacro} (@pxref{Extending pcase}).
504 508
505The rest of this subsection 509The rest of this subsection describes different forms of core
506describes different forms of core patterns, 510patterns, presents some examples, and concludes with important caveats
507presents some examples, 511on using the let-binding facility provided by some pattern forms. A
508and concludes with important caveats on using the 512core pattern can have the following forms:
509let-binding facility provided by some pattern forms.
510A core pattern can have the following forms:
511 513
512@table @code 514@table @code
513 515
514@item _ 516@item _
515Matches any @var{expval}. 517Matches any @var{expval}.
516This is known as @dfn{don't care} or @dfn{wildcard}. 518This is also known as @dfn{don't care} or @dfn{wildcard}.
517 519
518@item '@var{val} 520@item '@var{val}
519Matches if @var{expval} is @code{equal} to @var{val}. 521Matches if @var{expval} is equals @var{val}. The comparison is done
522as if by @code{equal} (@pxref{Equality Predicates}).
520 523
521@item @var{keyword} 524@item @var{keyword}
522@itemx @var{integer} 525@itemx @var{integer}
523@itemx @var{string} 526@itemx @var{string}
524Matches if @var{expval} is @code{equal} to the literal object. 527Matches if @var{expval} equals the literal object.
525This is a special case of @code{'@var{val}}, above, 528This is a special case of @code{'@var{val}}, above,
526possible because literal objects of these types are self-quoting. 529possible because literal objects of these types are self-quoting.
527 530
@@ -533,17 +536,17 @@ Matches any @var{expval}, and additionally let-binds @var{symbol} to
533If @var{symbol} is part of a sequencing pattern @var{seqpat} 536If @var{symbol} is part of a sequencing pattern @var{seqpat}
534(e.g., by using @code{and}, below), the binding is also available to 537(e.g., by using @code{and}, below), the binding is also available to
535the portion of @var{seqpat} following the appearance of @var{symbol}. 538the portion of @var{seqpat} following the appearance of @var{symbol}.
536This usage has some caveats (@pxref{pcase-symbol-caveats,,caveats}). 539This usage has some caveats, see @ref{pcase-symbol-caveats,,caveats}.
537 540
538Two symbols to avoid are @code{t}, which behaves like @code{_} 541Two symbols to avoid are @code{t}, which behaves like @code{_}
539(above) and is deprecated, and @code{nil}, which signals error. 542(above) and is deprecated, and @code{nil}, which signals an error.
540Likewise, it makes no sense to bind keyword symbols 543Likewise, it makes no sense to bind keyword symbols
541(@pxref{Constant Variables}). 544(@pxref{Constant Variables}).
542 545
543@item (pred @var{function}) 546@item (pred @var{function})
544Matches if the predicate @var{function} returns non-@code{nil} 547Matches if the predicate @var{function} returns non-@code{nil}
545when called on @var{expval}. 548when called on @var{expval}.
546@var{function} can have one of the possible forms: 549the predicate @var{function} can have one of the following forms:
547 550
548@table @asis 551@table @asis
549@item function name (a symbol) 552@item function name (a symbol)
@@ -570,20 +573,17 @@ the actual function call becomes: @w{@code{(= 42 @var{expval})}}.
570@item (app @var{function} @var{pattern}) 573@item (app @var{function} @var{pattern})
571Matches if @var{function} called on @var{expval} returns a 574Matches if @var{function} called on @var{expval} returns a
572value that matches @var{pattern}. 575value that matches @var{pattern}.
573@var{function} can take one of the 576@var{function} can take one of the forms described for @code{pred},
574forms described for @code{pred}, above. 577above. Unlike @code{pred}, however, @code{app} tests the result
575Unlike @code{pred}, however, 578against @var{pattern}, rather than against a boolean truth value.
576@code{app} tests the result against @var{pattern},
577rather than against a boolean truth value.
578 579
579@item (guard @var{boolean-expression}) 580@item (guard @var{boolean-expression})
580Matches if @var{boolean-expression} evaluates to non-@code{nil}. 581Matches if @var{boolean-expression} evaluates to non-@code{nil}.
581 582
582@item (let @var{pattern} @var{expr}) 583@item (let @var{pattern} @var{expr})
583Evaluates @var{expr} to get @var{exprval} 584Evaluates @var{expr} to get @var{exprval} and matches if @var{exprval}
584and matches if @var{exprval} matches @var{pattern}. 585matches @var{pattern}. (It is called @code{let} because @var{pattern}
585(It is called @code{let} because 586can bind symbols to values using @var{symbol}.)
586@var{pattern} can bind symbols to values using @var{symbol}.)
587@end table 587@end table
588 588
589@cindex sequencing pattern 589@cindex sequencing pattern
@@ -596,18 +596,16 @@ but instead of processing values, they process sub-patterns.
596 596
597@table @code 597@table @code
598@item (and @var{pattern1}@dots{}) 598@item (and @var{pattern1}@dots{})
599Attempts to match @var{pattern1}@dots{}, in order, 599Attempts to match @var{pattern1}@dots{}, in order, until one of them
600until one of them fails to match. 600fails to match. In that case, @code{and} likewise fails to match, and
601In that case, @code{and} likewise fails to match, 601the rest of the sub-patterns are not tested. If all sub-patterns
602and the rest of the sub-patterns are not tested. 602match, @code{and} matches.
603If all sub-patterns match, @code{and} matches.
604 603
605@item (or @var{pattern1} @var{pattern2}@dots{}) 604@item (or @var{pattern1} @var{pattern2}@dots{})
606Attempts to match @var{pattern1}, @var{pattern2}, @dots{}, in order, 605Attempts to match @var{pattern1}, @var{pattern2}, @dots{}, in order,
607until one of them succeeds. 606until one of them succeeds. In that case, @code{or} likewise matches,
608In that case, @code{or} likewise matches, 607and the rest of the sub-patterns are not tested. (Note that there
609and the rest of the sub-patterns are not tested. 608must be at least two sub-patterns.
610(Note that there must be at least two sub-patterns.
611Simply @w{@code{(or @var{pattern1})}} signals error.) 609Simply @w{@code{(or @var{pattern1})}} signals error.)
612@c Issue: Is this correct and intended? 610@c Issue: Is this correct and intended?
613@c Are there exceptions, qualifications? 611@c Are there exceptions, qualifications?
@@ -1042,12 +1040,11 @@ Both use a single backquote construct (@pxref{Backquote}).
1042 1040
1043This subsection describes @dfn{backquote-style patterns}, 1041This subsection describes @dfn{backquote-style patterns},
1044a set of builtin patterns that eases structural matching. 1042a set of builtin patterns that eases structural matching.
1045For background, @xref{Pattern-Matching Conditional}. 1043For background, @pxref{Pattern-Matching Conditional}.
1046 1044
1047@dfn{Backquote-style patterns} are a powerful set of 1045Backquote-style patterns are a powerful set of @code{pcase} pattern
1048@code{pcase} pattern extensions (created using @code{pcase-defmacro}) 1046extensions (created using @code{pcase-defmacro}) that make it easy to
1049that make it easy to match @var{expval} against 1047match @var{expval} against specifications of its @emph{structure}.
1050specifications of its @emph{structure}.
1051 1048
1052For example, to match @var{expval} that must be a list of two 1049For example, to match @var{expval} that must be a list of two
1053elements whose first element is a specific string and the second 1050elements whose first element is a specific string and the second
@@ -1173,87 +1170,102 @@ evaluation results:
1173(evaluate '(sub 1 2) nil) @result{} error 1170(evaluate '(sub 1 2) nil) @result{} error
1174@end example 1171@end example
1175 1172
1176@node Destructuring patterns 1173@node Destructuring with pcase Patterns
1177@subsection Destructuring Patterns 1174@subsection Destructuring with @code{pcase} Patterns
1178@cindex destructuring patterns 1175@cindex destructuring with pcase patterns
1179 1176
1180Pcase patterns not only express a condition on the form of the objects 1177Pcase patterns not only express a condition on the form of the objects
1181they can match but they can also extract sub-fields of those objects. 1178they can match, but they can also extract sub-fields of those objects.
1182Say we have a list and want to extract 2 elements from it with the 1179For example we can extract 2 elements from a list that is the value of
1183following code: 1180the variable @code{my-list} with the following code:
1184 1181
1185@example 1182@example
1186 (pcase l 1183 (pcase my-list
1187 (`(add ,x ,y) (message "Contains %S and %S" x y))) 1184 (`(add ,x ,y) (message "Contains %S and %S" x y)))
1188@end example 1185@end example
1189 1186
1190This will not only extract @code{x} and @code{y} but will additionally 1187This will not only extract @code{x} and @code{y} but will additionally
1191test that @code{l} is a list containing exactly 3 elements and whose 1188test that @code{my-list} is a list containing exactly 3 elements and
1192first element is the symbol @code{add}. If any of those tests fail, 1189whose first element is the symbol @code{add}. If any of those tests
1193@code{pcase} will directly return @code{nil} without calling 1190fail, @code{pcase} will immediately return @code{nil} without calling
1194@code{message}. 1191@code{message}.
1195 1192
1196@dfn{Destructuring} of an object is an operation that extracts 1193Extraction of multiple values stored in an object is known as
1197multiple values stored in the object, e.g., the 2nd and the 3rd 1194@dfn{destructuring}. Using @code{pcase} patterns allows to perform
1198element of a list or a vector. @dfn{Destructuring binding} is 1195@dfn{destructuring binding}, which is similar to a local binding
1199similar to a local binding (@pxref{Local Variables}), but it gives 1196(@pxref{Local Variables}), but gives values to multiple elements of
1200values to multiple elements of a variable by extracting those values 1197a variable by extracting those values from an object of compatible
1201from an object of compatible structure. 1198structure.
1202 1199
1203The macros described in this section use @dfn{destructuring 1200The macros described in this section use @code{pcase} patterns to
1204patterns}, which are normal Pcase patterns used in a context where we 1201perform destructuring binding. The condition of the object to be of
1205presume that the object does match the pattern, and we only want 1202compatible structure means that the object must match the pattern,
1206to extract some subfields. For example: 1203because only then the object's subfields can be extracted. For
1204example:
1207 1205
1208@example 1206@example
1209 (pcase-let ((`(add ,x ,y) l)) 1207 (pcase-let ((`(add ,x ,y) my-list))
1210 (message "Contains %S and %S" x y)) 1208 (message "Contains %S and %S" x y))
1211@end example 1209@end example
1212 1210
1213@noindent 1211@noindent
1214does the same as the previous example, except that it directly tries 1212does the same as the previous example, except that it directly tries
1215to extract @code{x} and @code{y} from @code{l} without first verifying 1213to extract @code{x} and @code{y} from @code{my-list} without first
1216if @code{l} is a list which has the right number of elements and has 1214verifying if @code{my-list} is a list which has the right number of
1217@code{add} as its first element. 1215elements and has @code{add} as its first element. The precise
1218The precise behavior when the object does not actually match the 1216behavior when the object does not actually match the pattern is
1219pattern is undefined, although the body will not be silently skipped: 1217undefined, although the body will not be silently skipped: either an
1220either an error is signaled or the body is run with some of the 1218error is signaled or the body is run with some of the variables
1221variables potentially bound to arbitrary values like @code{nil}. 1219potentially bound to arbitrary values like @code{nil}.
1220
1221The pcase patterns that are useful for destructuring bindings are
1222generally those described in @ref{Backquote Patterns}, since they
1223express a specification of the structure of objects that will match.
1224
1225For an alternative facility for destructuring binding, see
1226@ref{seq-let}.
1222 1227
1223@defmac pcase-let bindings body@dots{} 1228@defmac pcase-let bindings body@dots{}
1224Bind variables according to @var{bindings} and then eval @var{body}. 1229Perform desctructuring binding of variables according to
1230@var{bindings}, and then evaluate @var{body}.
1225 1231
1226@var{bindings} is a list of bindings of the form @w{@code{(@var{pattern} 1232@var{bindings} is a list of bindings of the form @w{@code{(@var{pattern}
1227@var{exp})}}, where @var{exp} is an expression to evaluate and 1233@var{exp})}}, where @var{exp} is an expression to evaluate and
1228@var{pattern} is a destructuring pattern. 1234@var{pattern} is a @code{pcase} pattern.
1229 1235
1230All @var{exp}s are evaluated first after which they are matched 1236All @var{exp}s are evaluated first, after which they are matched
1231against their respective @var{pattern}, introducing new variable 1237against their respective @var{pattern}, introducing new variable
1232bindings which can then be used inside @var{body}. 1238bindings that can then be used inside @var{body}. The variable
1239bindings are produced by destructuring binding of elements of
1240@var{pattern} to the values of the corresponding elements of the
1241evaluated @var{exp}.
1233@end defmac 1242@end defmac
1234 1243
1235@defmac pcase-let* bindings body@dots{} 1244@defmac pcase-let* bindings body@dots{}
1236Bind variables according to @var{bindings} and then eval @var{body}. 1245Perform desctructuring binding of variables according to
1246@var{bindings}, and then evaluate @var{body}.
1237 1247
1238@var{bindings} is a list of bindings of the form @code{(@var{pattern} 1248@var{bindings} is a list of bindings of the form @code{(@var{pattern}
1239@var{exp})}, where @var{exp} is an expression to evaluate and 1249@var{exp})}, where @var{exp} is an expression to evaluate and
1240@var{pattern} is a destructuring pattern. 1250@var{pattern} is a @code{pcase} pattern. The variable bindings are
1241 1251produced by destructuring binding of elements of @var{pattern} to the
1242Unlike @code{pcase-let}, but like @code{let*}, each @var{exp} is 1252values of the corresponding elements of the evaluated @var{exp}.
1243matched against its corresponding @var{pattern} before passing to the 1253
1244next element of @var{bindings}, so the variables introduced in each 1254Unlike @code{pcase-let}, but similarly to @code{let*}, each @var{exp}
1245binding are available in the @var{exp}s that follow it, additionally 1255is matched against its corresponding @var{pattern} before processing
1246to being available in @var{body}. 1256the next element of @var{bindings}, so the variable bindings
1257introduced in each one of the @var{bindings} are available in the
1258@var{exp}s of the @var{bindings} that follow it, additionally to
1259being available in @var{body}.
1247@end defmac 1260@end defmac
1248 1261
1249@findex dolist
1250@defmac pcase-dolist (pattern list) body@dots{} 1262@defmac pcase-dolist (pattern list) body@dots{}
1251This construct executes @var{body} once for each element of 1263Execute @var{body} once for each element of @var{list}, on each
1252@var{list}, in a context where the variables appearing in the the 1264iteration performing a destructuring binding of variables in
1253destructuring pattern @var{pattern} are bound to the corresponding 1265@var{pattern} to the values of the corresponding subfields of the
1254values found in the element. 1266element of @var{list}. The bindings are performed as if by
1255When @var{pattern} is a simple variable, this ends up being equivalent 1267@code{pcase-let}. When @var{pattern} is a simple variable, this ends
1256to @code{dolist}. 1268up being equivalent to @code{dolist} (@pxref{Iteration}).
1257@end defmac 1269@end defmac
1258 1270
1259 1271
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 51d724cb1d8..60d017c3e44 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -1049,15 +1049,18 @@ that @var{sequence} can be a list, vector or string. This is
1049primarily useful for side-effects. 1049primarily useful for side-effects.
1050@end defmac 1050@end defmac
1051 1051
1052@defmac seq-let arguments sequence body@dots{} 1052@anchor{seq-let}
1053@defmac seq-let var-sequence val-sequence body@dots{}
1053@cindex sequence destructuring 1054@cindex sequence destructuring
1054 This macro binds the variables defined in @var{arguments} to the 1055 This macro binds the variables defined in @var{var-sequence} to the
1055elements of @var{sequence}. @var{arguments} can themselves include 1056values that are the corresponding elements of @var{val-sequence}.
1056sequences, allowing for nested destructuring. 1057This is known as @dfn{destructuring binding}. The elements of
1058@var{var-sequence} can themselves include sequences, allowing for
1059nested destructuring.
1057 1060
1058The @var{arguments} sequence can also include the @code{&rest} marker 1061The @var{var-sequence} sequence can also include the @code{&rest}
1059followed by a variable name to be bound to the rest of 1062marker followed by a variable name to be bound to the rest of
1060@code{sequence}. 1063@var{val-sequence}.
1061 1064
1062@example 1065@example
1063@group 1066@group
@@ -1081,6 +1084,9 @@ followed by a variable name to be bound to the rest of
1081@end group 1084@end group
1082@result{} [3 4] 1085@result{} [3 4]
1083@end example 1086@end example
1087
1088The @code{pcase} patterns provide an alternative facility for
1089destructuring binding, see @ref{Destructuring with pcase Patterns}.
1084@end defmac 1090@end defmac
1085 1091
1086@defun seq-random-elt sequence 1092@defun seq-random-elt sequence
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index 2e89ae0779a..fde3bdb27f3 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -264,10 +264,14 @@ variable name being but a special case of it)."
264 264
265;;;###autoload 265;;;###autoload
266(defmacro pcase-let* (bindings &rest body) 266(defmacro pcase-let* (bindings &rest body)
267 "Like `let*' but where you can use `pcase' patterns for bindings. 267 "Like `let*', but supports destructuring BINDINGS using `pcase' patterns.
268BODY should be an expression, and BINDINGS should be a list of bindings 268As with `pcase-let', BINDINGS are of the form (PATTERN EXP), but the
269of the form (PATTERN EXP). 269EXP in each binding in BINDINGS can use the results of the destructuring
270See `pcase-let' for discussion of how PATTERN is matched." 270bindings that precede it in BINDINGS' order.
271
272Each EXP should match (i.e. be of compatible structure) to its
273respective PATTERN; a mismatch may signal an error or may go
274undetected, binding variables to arbitrary values, such as nil."
271 (declare (indent 1) 275 (declare (indent 1)
272 (debug ((&rest (pcase-PAT &optional form)) body))) 276 (debug ((&rest (pcase-PAT &optional form)) body)))
273 (let ((cached (gethash bindings pcase--memoize))) 277 (let ((cached (gethash bindings pcase--memoize)))
@@ -280,13 +284,16 @@ See `pcase-let' for discussion of how PATTERN is matched."
280 284
281;;;###autoload 285;;;###autoload
282(defmacro pcase-let (bindings &rest body) 286(defmacro pcase-let (bindings &rest body)
283 "Like `let' but where you can use `pcase' patterns for bindings. 287 "Like `let', but supports destructuring BINDINGS using `pcase' patterns.
284BODY should be a list of expressions, and BINDINGS should be a list of bindings 288BODY should be a list of expressions, and BINDINGS should be a list of
285of the form (PATTERN EXP). 289bindings of the form (PATTERN EXP).
286The PATTERNs are only used to extract data, so the code does not test 290All EXPs are evaluated first, and then used to perform destructuring
287whether the data does match the corresponding patterns: a mismatch 291bindings by matching each EXP against its respective PATTERN. Then
288may signal an error or may go undetected, binding variables to arbitrary 292BODY is evaluated with those bindings in effect.
289values, such as nil." 293
294Each EXP should match (i.e. be of compatible structure) to its
295respective PATTERN; a mismatch may signal an error or may go
296undetected, binding variables to arbitrary values, such as nil."
290 (declare (indent 1) (debug pcase-let*)) 297 (declare (indent 1) (debug pcase-let*))
291 (if (null (cdr bindings)) 298 (if (null (cdr bindings))
292 `(pcase-let* ,bindings ,@body) 299 `(pcase-let* ,bindings ,@body)
@@ -304,11 +311,15 @@ values, such as nil."
304 311
305;;;###autoload 312;;;###autoload
306(defmacro pcase-dolist (spec &rest body) 313(defmacro pcase-dolist (spec &rest body)
307 "Superset of `dolist' where the VAR binding can be a `pcase' PATTERN. 314 "Eval BODY once for each set of bindings defined by PATTERN and LIST elements.
308More specifically, this is just a shorthand for the following combination 315PATTERN should be a `pcase' pattern describing the structure of
309of `dolist' and `pcase-let': 316LIST elements, and LIST is a list of objects that match PATTERN,
310 317i.e. have a structure that is compatible with PATTERN.
311 (dolist (x LIST) (pcase-let ((PATTERN x)) BODY...)) 318For each element of LIST, this macro binds the variables in
319PATTERN to the corresponding subfields of the LIST element, and
320then evaluates BODY with these bindings in effect. The
321destructuring bindings of variables in PATTERN to the subfields
322of the elements of LIST is performed as if by `pcase-let'.
312\n(fn (PATTERN LIST) BODY...)" 323\n(fn (PATTERN LIST) BODY...)"
313 (declare (indent 1) (debug ((pcase-PAT form) body))) 324 (declare (indent 1) (debug ((pcase-PAT form) body)))
314 (if (pcase--trivial-upat-p (car spec)) 325 (if (pcase--trivial-upat-p (car spec))