aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2018-10-31 15:34:45 -0400
committerStefan Monnier2018-10-31 15:34:45 -0400
commit9962cf959fd2edf7a68036ca9a81c0bbe35b67df (patch)
tree96065caa1ca86fbb26f9411a48f81d59532f461e
parentc3cf85b1c186e13c2d588aa35ffa57981ca481d7 (diff)
downloademacs-9962cf959fd2edf7a68036ca9a81c0bbe35b67df.tar.gz
emacs-9962cf959fd2edf7a68036ca9a81c0bbe35b67df.zip
* doc/lispref/control.texi (Destructuring patterns): New subsection.
-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 5be4b298b46..06c6622bf01 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,
@@ -1168,6 +1173,90 @@ evaluation results:
1168(evaluate '(sub 1 2) nil) @result{} error 1173(evaluate '(sub 1 2) nil) @result{} error
1169@end example 1174@end example
1170 1175
1176@node Destructuring patterns
1177@subsection Destructuring Patterns
1178@cindex destructuring patterns
1179
1180Pcase 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.
1182Say we have a list and want to extract 2 elements from it with the
1183following code:
1184
1185@example
1186 (pcase l
1187 (`(add ,x ,y) (message "Contains %S and %S" x y)))
1188@end example
1189
1190This 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
1192first element is the symbol @code{add}. If any of those tests fail,
1193@code{pcase} will directly return @code{nil} without calling
1194@code{message}.
1195
1196@dfn{Destructuring} of an object is an operation that extracts
1197multiple values stored in the object, e.g., the 2nd and the 3rd
1198element of a list or a vector. @dfn{Destructuring binding} is
1199similar to a local binding (@pxref{Local Variables}), but it gives
1200values to multiple elements of a variable by extracting those values
1201from an object of compatible structure.
1202
1203The macros described in this section use @dfn{destructuring
1204patterns}, which are normal Pcase patterns used in a context where we
1205presume that the object does match the pattern, and we only want
1206to extract some subfields. For example:
1207
1208@example
1209 (pcase-let ((`(add ,x ,y) l))
1210 (message "Contains %S and %S" x y))
1211@end example
1212
1213@noindent
1214does the same as the previous example, except that it directly tries
1215to extract @code{x} and @code{y} from @code{l} without first verifying
1216if @code{l} is a list which has the right number of elements and has
1217@code{add} as its first element.
1218The precise behavior when the object does not actually match the
1219pattern is undefined, although the body will not be silently skipped:
1220either an error is signaled or the body is run with some of the
1221variables potentially bound to arbitrary values like @code{nil}.
1222
1223@defmac pcase-let bindings body@dots{}
1224Bind variables according to @var{bindings} and then eval @var{body}.
1225
1226@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
1228@var{pattern} is a destructuring pattern.
1229
1230All @var{exp}s are evaluated first after which they are matched
1231against their respective @var{pattern}, introducing new variable
1232bindings which can then be used inside @var{body}.
1233@end defmac
1234
1235@defmac pcase-let* bindings body@dots{}
1236Bind variables according to @var{bindings} and then eval @var{body}.
1237
1238@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
1240@var{pattern} is a destructuring pattern.
1241
1242Unlike @code{pcase-let}, but like @code{let*}, each @var{exp} is
1243matched against its corresponding @var{pattern} before passing to the
1244next element of @var{bindings}, so the variables introduced in each
1245binding are available in the @var{exp}s that follow it, additionally
1246to being available in @var{body}.
1247@end defmac
1248
1249@findex dolist
1250@defmac pcase-dolist (pattern list) body@dots{}
1251This construct executes @var{body} once for each element of
1252@var{list}, in a context where the variables appearing in the the
1253destructuring pattern @var{pattern} are bound to the corresponding
1254values found in the element.
1255When @var{pattern} is a simple variable, this ends up being equivalent
1256to @code{dolist}.
1257@end defmac
1258
1259
1171@node Iteration 1260@node Iteration
1172@section Iteration 1261@section Iteration
1173@cindex iteration 1262@cindex iteration