aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlenn Morris2018-11-04 09:37:03 -0800
committerGlenn Morris2018-11-04 09:37:03 -0800
commit9b90f1b6be09abc31af02a9f09390bdcc8922c6e (patch)
tree1b48ec5f628e73e471a6d503cf0af1aaa6d72a34
parent19d2ba00596b8ee31cff046b2387580b016fa4c5 (diff)
parent9962cf959fd2edf7a68036ca9a81c0bbe35b67df (diff)
downloademacs-9b90f1b6be09abc31af02a9f09390bdcc8922c6e.tar.gz
emacs-9b90f1b6be09abc31af02a9f09390bdcc8922c6e.zip
Merge from origin/emacs-26
9962cf9 * doc/lispref/control.texi (Destructuring patterns): New subs...
-rw-r--r--doc/lispref/control.texi89
1 files changed, 89 insertions, 0 deletions
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi
index 0f7502f1c20..e5041ee627b 100644
--- a/doc/lispref/control.texi
+++ b/doc/lispref/control.texi
@@ -477,6 +477,7 @@ returns non-@code{nil}, the pattern matches the value
477* The @code{pcase} macro: pcase Macro. Plus examples and caveats. 477* The @code{pcase} macro: pcase Macro. Plus examples and caveats.
478* Extending @code{pcase}: Extending pcase. Define new kinds of patterns. 478* Extending @code{pcase}: Extending pcase. Define new kinds of patterns.
479* Backquote-Style Patterns: Backquote Patterns. Structural matching. 479* Backquote-Style Patterns: Backquote Patterns. Structural matching.
480* Destructuring patterns:: Using pcase patterns to extract subfields.
480@end menu 481@end menu
481 482
482@node pcase Macro 483@node pcase Macro
@@ -497,6 +498,10 @@ of the last of @var{body-forms} in the successful clause.
497Otherwise, @code{pcase} evaluates to @code{nil}. 498Otherwise, @code{pcase} evaluates to @code{nil}.
498@end defmac 499@end defmac
499 500
501Each @var{pattern} has to be a @dfn{pcase pattern}, which can either
502use one of the core patterns defined below, or use one of the patterns
503defined via @code{pcase-defmacro}.
504
500The rest of this subsection 505The rest of this subsection
501describes different forms of core patterns, 506describes different forms of core patterns,
502presents some examples, 507presents some examples,
@@ -1166,6 +1171,90 @@ evaluation results:
1166(evaluate '(sub 1 2) nil) @result{} error 1171(evaluate '(sub 1 2) nil) @result{} error
1167@end example 1172@end example
1168 1173
1174@node Destructuring patterns
1175@subsection Destructuring Patterns
1176@cindex destructuring patterns
1177
1178Pcase patterns not only express a condition on the form of the objects
1179they can match but they can also extract sub-fields of those objects.
1180Say we have a list and want to extract 2 elements from it with the
1181following code:
1182
1183@example
1184 (pcase l
1185 (`(add ,x ,y) (message "Contains %S and %S" x y)))
1186@end example
1187
1188This will not only extract @code{x} and @code{y} but will additionally
1189test that @code{l} is a list containing exactly 3 elements and whose
1190first element is the symbol @code{add}. If any of those tests fail,
1191@code{pcase} will directly return @code{nil} without calling
1192@code{message}.
1193
1194@dfn{Destructuring} of an object is an operation that extracts
1195multiple values stored in the object, e.g., the 2nd and the 3rd
1196element of a list or a vector. @dfn{Destructuring binding} is
1197similar to a local binding (@pxref{Local Variables}), but it gives
1198values to multiple elements of a variable by extracting those values
1199from an object of compatible structure.
1200
1201The macros described in this section use @dfn{destructuring
1202patterns}, which are normal Pcase patterns used in a context where we
1203presume that the object does match the pattern, and we only want
1204to extract some subfields. For example:
1205
1206@example
1207 (pcase-let ((`(add ,x ,y) l))
1208 (message "Contains %S and %S" x y))
1209@end example
1210
1211@noindent
1212does the same as the previous example, except that it directly tries
1213to extract @code{x} and @code{y} from @code{l} without first verifying
1214if @code{l} is a list which has the right number of elements and has
1215@code{add} as its first element.
1216The precise behavior when the object does not actually match the
1217pattern is undefined, although the body will not be silently skipped:
1218either an error is signaled or the body is run with some of the
1219variables potentially bound to arbitrary values like @code{nil}.
1220
1221@defmac pcase-let bindings body@dots{}
1222Bind variables according to @var{bindings} and then eval @var{body}.
1223
1224@var{bindings} is a list of bindings of the form @w{@code{(@var{pattern}
1225@var{exp})}}, where @var{exp} is an expression to evaluate and
1226@var{pattern} is a destructuring pattern.
1227
1228All @var{exp}s are evaluated first after which they are matched
1229against their respective @var{pattern}, introducing new variable
1230bindings which can then be used inside @var{body}.
1231@end defmac
1232
1233@defmac pcase-let* bindings body@dots{}
1234Bind variables according to @var{bindings} and then eval @var{body}.
1235
1236@var{bindings} is a list of bindings of the form @code{(@var{pattern}
1237@var{exp})}, where @var{exp} is an expression to evaluate and
1238@var{pattern} is a destructuring pattern.
1239
1240Unlike @code{pcase-let}, but like @code{let*}, each @var{exp} is
1241matched against its corresponding @var{pattern} before passing to the
1242next element of @var{bindings}, so the variables introduced in each
1243binding are available in the @var{exp}s that follow it, additionally
1244to being available in @var{body}.
1245@end defmac
1246
1247@findex dolist
1248@defmac pcase-dolist (pattern list) body@dots{}
1249This construct executes @var{body} once for each element of
1250@var{list}, in a context where the variables appearing in the the
1251destructuring pattern @var{pattern} are bound to the corresponding
1252values found in the element.
1253When @var{pattern} is a simple variable, this ends up being equivalent
1254to @code{dolist}.
1255@end defmac
1256
1257
1169@node Iteration 1258@node Iteration
1170@section Iteration 1259@section Iteration
1171@cindex iteration 1260@cindex iteration