diff options
| author | Glenn Morris | 2018-11-04 09:37:03 -0800 |
|---|---|---|
| committer | Glenn Morris | 2018-11-04 09:37:03 -0800 |
| commit | 9b90f1b6be09abc31af02a9f09390bdcc8922c6e (patch) | |
| tree | 1b48ec5f628e73e471a6d503cf0af1aaa6d72a34 | |
| parent | 19d2ba00596b8ee31cff046b2387580b016fa4c5 (diff) | |
| parent | 9962cf959fd2edf7a68036ca9a81c0bbe35b67df (diff) | |
| download | emacs-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.texi | 89 |
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. | |||
| 497 | Otherwise, @code{pcase} evaluates to @code{nil}. | 498 | Otherwise, @code{pcase} evaluates to @code{nil}. |
| 498 | @end defmac | 499 | @end defmac |
| 499 | 500 | ||
| 501 | Each @var{pattern} has to be a @dfn{pcase pattern}, which can either | ||
| 502 | use one of the core patterns defined below, or use one of the patterns | ||
| 503 | defined via @code{pcase-defmacro}. | ||
| 504 | |||
| 500 | The rest of this subsection | 505 | The rest of this subsection |
| 501 | describes different forms of core patterns, | 506 | describes different forms of core patterns, |
| 502 | presents some examples, | 507 | presents 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 | |||
| 1178 | Pcase patterns not only express a condition on the form of the objects | ||
| 1179 | they can match but they can also extract sub-fields of those objects. | ||
| 1180 | Say we have a list and want to extract 2 elements from it with the | ||
| 1181 | following code: | ||
| 1182 | |||
| 1183 | @example | ||
| 1184 | (pcase l | ||
| 1185 | (`(add ,x ,y) (message "Contains %S and %S" x y))) | ||
| 1186 | @end example | ||
| 1187 | |||
| 1188 | This will not only extract @code{x} and @code{y} but will additionally | ||
| 1189 | test that @code{l} is a list containing exactly 3 elements and whose | ||
| 1190 | first 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 | ||
| 1195 | multiple values stored in the object, e.g., the 2nd and the 3rd | ||
| 1196 | element of a list or a vector. @dfn{Destructuring binding} is | ||
| 1197 | similar to a local binding (@pxref{Local Variables}), but it gives | ||
| 1198 | values to multiple elements of a variable by extracting those values | ||
| 1199 | from an object of compatible structure. | ||
| 1200 | |||
| 1201 | The macros described in this section use @dfn{destructuring | ||
| 1202 | patterns}, which are normal Pcase patterns used in a context where we | ||
| 1203 | presume that the object does match the pattern, and we only want | ||
| 1204 | to 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 | ||
| 1212 | does the same as the previous example, except that it directly tries | ||
| 1213 | to extract @code{x} and @code{y} from @code{l} without first verifying | ||
| 1214 | if @code{l} is a list which has the right number of elements and has | ||
| 1215 | @code{add} as its first element. | ||
| 1216 | The precise behavior when the object does not actually match the | ||
| 1217 | pattern is undefined, although the body will not be silently skipped: | ||
| 1218 | either an error is signaled or the body is run with some of the | ||
| 1219 | variables potentially bound to arbitrary values like @code{nil}. | ||
| 1220 | |||
| 1221 | @defmac pcase-let bindings body@dots{} | ||
| 1222 | Bind 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 | |||
| 1228 | All @var{exp}s are evaluated first after which they are matched | ||
| 1229 | against their respective @var{pattern}, introducing new variable | ||
| 1230 | bindings which can then be used inside @var{body}. | ||
| 1231 | @end defmac | ||
| 1232 | |||
| 1233 | @defmac pcase-let* bindings body@dots{} | ||
| 1234 | Bind 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 | |||
| 1240 | Unlike @code{pcase-let}, but like @code{let*}, each @var{exp} is | ||
| 1241 | matched against its corresponding @var{pattern} before passing to the | ||
| 1242 | next element of @var{bindings}, so the variables introduced in each | ||
| 1243 | binding are available in the @var{exp}s that follow it, additionally | ||
| 1244 | to being available in @var{body}. | ||
| 1245 | @end defmac | ||
| 1246 | |||
| 1247 | @findex dolist | ||
| 1248 | @defmac pcase-dolist (pattern list) body@dots{} | ||
| 1249 | This construct executes @var{body} once for each element of | ||
| 1250 | @var{list}, in a context where the variables appearing in the the | ||
| 1251 | destructuring pattern @var{pattern} are bound to the corresponding | ||
| 1252 | values found in the element. | ||
| 1253 | When @var{pattern} is a simple variable, this ends up being equivalent | ||
| 1254 | to @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 |