diff options
| author | Paul Eggert | 2020-04-19 12:00:49 -0700 |
|---|---|---|
| committer | Paul Eggert | 2020-04-19 13:29:57 -0700 |
| commit | dca35b31d0a58efdcc698faf90493b96fa8e1406 (patch) | |
| tree | 7b40c27250607e6010152121d7dc5dc2624532fd /doc | |
| parent | 81e7d7f111872c9f2aaf8885db50a22ed746d7b5 (diff) | |
| download | emacs-dca35b31d0a58efdcc698faf90493b96fa8e1406.tar.gz emacs-dca35b31d0a58efdcc698faf90493b96fa8e1406.zip | |
Improve mutability documentation
This change was inspired by comments from Štěpán Němec in:
https://lists.gnu.org/r/emacs-devel/2020-04/msg01063.html
* doc/lispref/objects.texi (Lisp Data Types): Mention mutability.
(Constants and mutability): New section.
* doc/lispintro/emacs-lisp-intro.texi (Lists diagrammed)
(Indent Tabs Mode): Improve wording.
* doc/lispref/eval.texi (Self-Evaluating Forms):
Say that they return constants.
* doc/lispref/lists.texi (Sets And Lists):
Fix memql mistake/confusion that I recently introduced.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispintro/emacs-lisp-intro.texi | 4 | ||||
| -rw-r--r-- | doc/lispref/elisp.texi | 1 | ||||
| -rw-r--r-- | doc/lispref/eval.texi | 7 | ||||
| -rw-r--r-- | doc/lispref/lists.texi | 16 | ||||
| -rw-r--r-- | doc/lispref/objects.texi | 45 | ||||
| -rw-r--r-- | doc/lispref/sequences.texi | 10 | ||||
| -rw-r--r-- | doc/lispref/strings.texi | 1 |
7 files changed, 69 insertions, 15 deletions
diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi index 630676d9786..ea16d9ef155 100644 --- a/doc/lispintro/emacs-lisp-intro.texi +++ b/doc/lispintro/emacs-lisp-intro.texi | |||
| @@ -9557,7 +9557,7 @@ part of which is the address of the next pair. The very last box | |||
| 9557 | points to the symbol @code{nil}, which marks the end of the list. | 9557 | points to the symbol @code{nil}, which marks the end of the list. |
| 9558 | 9558 | ||
| 9559 | @need 1200 | 9559 | @need 1200 |
| 9560 | When a variable is set to a list via @code{setq}, | 9560 | When a variable is set to a list with an operation such as @code{setq}, |
| 9561 | it stores the address of the first box in the variable. Thus, | 9561 | it stores the address of the first box in the variable. Thus, |
| 9562 | evaluation of the expression | 9562 | evaluation of the expression |
| 9563 | 9563 | ||
| @@ -17140,7 +17140,7 @@ The following turns off Indent Tabs mode: | |||
| 17140 | @end smallexample | 17140 | @end smallexample |
| 17141 | 17141 | ||
| 17142 | Note that this line uses @code{setq-default} rather than the | 17142 | Note that this line uses @code{setq-default} rather than the |
| 17143 | @code{setq} that we have seen before. The @code{setq-default} | 17143 | @code{setq} that we have seen before; @code{setq-default} |
| 17144 | sets values only in buffers that do not have their own local | 17144 | sets values only in buffers that do not have their own local |
| 17145 | values for the variable. | 17145 | values for the variable. |
| 17146 | 17146 | ||
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi index cfd96f7aa68..bba1b63115f 100644 --- a/doc/lispref/elisp.texi +++ b/doc/lispref/elisp.texi | |||
| @@ -297,6 +297,7 @@ Lisp Data Types | |||
| 297 | * Circular Objects:: Read syntax for circular structure. | 297 | * Circular Objects:: Read syntax for circular structure. |
| 298 | * Type Predicates:: Tests related to types. | 298 | * Type Predicates:: Tests related to types. |
| 299 | * Equality Predicates:: Tests of equality between any two objects. | 299 | * Equality Predicates:: Tests of equality between any two objects. |
| 300 | * Constants and Mutability:: Whether an object's value can change. | ||
| 300 | 301 | ||
| 301 | Programming Types | 302 | Programming Types |
| 302 | 303 | ||
diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi index 46cfab164b7..deb288943af 100644 --- a/doc/lispref/eval.texi +++ b/doc/lispref/eval.texi | |||
| @@ -158,6 +158,12 @@ contents unchanged. | |||
| 158 | @end group | 158 | @end group |
| 159 | @end example | 159 | @end example |
| 160 | 160 | ||
| 161 | A self-evaluating form yields a constant, and you should not attempt | ||
| 162 | to modify the constant's contents via @code{setcar}, @code{aset} or | ||
| 163 | similar primitives. The Lisp interpreter might unify the constants | ||
| 164 | yielded by your program's self-evaluating forms, so that these | ||
| 165 | constants might share structure. @xref{Constants and Mutability}. | ||
| 166 | |||
| 161 | It is common to write numbers, characters, strings, and even vectors | 167 | It is common to write numbers, characters, strings, and even vectors |
| 162 | in Lisp code, taking advantage of the fact that they self-evaluate. | 168 | in Lisp code, taking advantage of the fact that they self-evaluate. |
| 163 | However, it is quite unusual to do this for types that lack a read | 169 | However, it is quite unusual to do this for types that lack a read |
| @@ -559,6 +565,7 @@ and vectors.) | |||
| 559 | @defspec quote object | 565 | @defspec quote object |
| 560 | This special form returns @var{object}, without evaluating it. | 566 | This special form returns @var{object}, without evaluating it. |
| 561 | The returned value is a constant, and should not be modified. | 567 | The returned value is a constant, and should not be modified. |
| 568 | @xref{Constants and Mutability}. | ||
| 562 | @end defspec | 569 | @end defspec |
| 563 | 570 | ||
| 564 | @cindex @samp{'} for quoting | 571 | @cindex @samp{'} for quoting |
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index c2771b01652..f1acc85616f 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi | |||
| @@ -866,16 +866,15 @@ foo ;; @r{@code{foo} was changed.} | |||
| 866 | @node Modifying Lists | 866 | @node Modifying Lists |
| 867 | @section Modifying Existing List Structure | 867 | @section Modifying Existing List Structure |
| 868 | @cindex destructive list operations | 868 | @cindex destructive list operations |
| 869 | @cindex constant lists | ||
| 870 | @cindex mutable lists | 869 | @cindex mutable lists |
| 871 | 870 | ||
| 872 | You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the | 871 | You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the |
| 873 | primitives @code{setcar} and @code{setcdr}. These are destructive | 872 | primitives @code{setcar} and @code{setcdr}. These are destructive |
| 874 | operations because they change existing list structure. | 873 | operations because they change existing list structure. |
| 875 | Destructive operations should be applied only to @dfn{mutable} lists, | 874 | Destructive operations should be applied only to mutable lists, |
| 876 | that is, lists constructed via @code{cons}, @code{list} or similar | 875 | that is, lists constructed via @code{cons}, @code{list} or similar |
| 877 | operations. Lists created by quoting are constants and should not be | 876 | operations. Lists created by quoting are constants and should not be |
| 878 | changed by destructive operations. | 877 | changed by destructive operations. @xref{Constants and Mutability}. |
| 879 | 878 | ||
| 880 | @cindex CL note---@code{rplaca} vs @code{setcar} | 879 | @cindex CL note---@code{rplaca} vs @code{setcar} |
| 881 | @quotation | 880 | @quotation |
| @@ -1169,9 +1168,9 @@ x | |||
| 1169 | @end group | 1168 | @end group |
| 1170 | @end example | 1169 | @end example |
| 1171 | 1170 | ||
| 1172 | However, the other arguments (all but the last) must be mutable lists. | 1171 | However, the other arguments (all but the last) should be mutable lists. |
| 1173 | 1172 | ||
| 1174 | A common pitfall is to use a quoted constant list as a non-last | 1173 | A common pitfall is to use a constant list as a non-last |
| 1175 | argument to @code{nconc}. If you do this, the resulting behavior | 1174 | argument to @code{nconc}. If you do this, the resulting behavior |
| 1176 | is undefined. It is possible that your program will change | 1175 | is undefined. It is possible that your program will change |
| 1177 | each time you run it! Here is what might happen (though this | 1176 | each time you run it! Here is what might happen (though this |
| @@ -1359,12 +1358,12 @@ Compare this with @code{memq}: | |||
| 1359 | 1358 | ||
| 1360 | @example | 1359 | @example |
| 1361 | @group | 1360 | @group |
| 1362 | (memql 1.2 '(1.1 1.2 1.3)) ; @r{@code{1.2} and @code{1.2} are @code{eql}.} | 1361 | (memql 1.2 '(1.1 1.2 1.3)) ; @r{@code{1.2} and @code{1.2} must be @code{eql}.} |
| 1363 | @result{} (1.2 1.3) | 1362 | @result{} (1.2 1.3) |
| 1364 | @end group | 1363 | @end group |
| 1365 | @group | 1364 | @group |
| 1366 | (memq (list 2) '((1) (2))) ; @r{@code{(list 2)} and @code{(2)} are not @code{eq}.} | 1365 | (memq 1.2 '(1.1 1.2 1.3)) ; @r{@code{1.2} and @code{1.2} need not be @code{eq}.} |
| 1367 | @result{} nil | 1366 | @result{} nil ; @r{... or it might be @code{(1.2 1.3)}.} |
| 1368 | @end group | 1367 | @end group |
| 1369 | @end example | 1368 | @end example |
| 1370 | @end defun | 1369 | @end defun |
| @@ -1628,6 +1627,7 @@ keys may not be symbols: | |||
| 1628 | '(("simple leaves" . oak) | 1627 | '(("simple leaves" . oak) |
| 1629 | ("compound leaves" . horsechestnut))) | 1628 | ("compound leaves" . horsechestnut))) |
| 1630 | 1629 | ||
| 1630 | ;; @r{The @code{copy-sequence} means the keys are not @code{eq}.} | ||
| 1631 | (assq (copy-sequence "simple leaves") leaves) | 1631 | (assq (copy-sequence "simple leaves") leaves) |
| 1632 | @result{} nil | 1632 | @result{} nil |
| 1633 | (assoc (copy-sequence "simple leaves") leaves) | 1633 | (assoc (copy-sequence "simple leaves") leaves) |
diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi index 1c4e7e4d4e3..98b001afd2d 100644 --- a/doc/lispref/objects.texi +++ b/doc/lispref/objects.texi | |||
| @@ -46,6 +46,10 @@ you store in it, type and all. (Actually, a small number of Emacs | |||
| 46 | Lisp variables can only take on values of a certain type. | 46 | Lisp variables can only take on values of a certain type. |
| 47 | @xref{Variables with Restricted Values}.) | 47 | @xref{Variables with Restricted Values}.) |
| 48 | 48 | ||
| 49 | Some Lisp objects are @dfn{constant}: their values never change. | ||
| 50 | Others are @dfn{mutable}: their values can be changed via destructive | ||
| 51 | operations that involve side effects. | ||
| 52 | |||
| 49 | This chapter describes the purpose, printed representation, and read | 53 | This chapter describes the purpose, printed representation, and read |
| 50 | syntax of each of the standard types in GNU Emacs Lisp. Details on how | 54 | syntax of each of the standard types in GNU Emacs Lisp. Details on how |
| 51 | to use these types can be found in later chapters. | 55 | to use these types can be found in later chapters. |
| @@ -59,6 +63,7 @@ to use these types can be found in later chapters. | |||
| 59 | * Circular Objects:: Read syntax for circular structure. | 63 | * Circular Objects:: Read syntax for circular structure. |
| 60 | * Type Predicates:: Tests related to types. | 64 | * Type Predicates:: Tests related to types. |
| 61 | * Equality Predicates:: Tests of equality between any two objects. | 65 | * Equality Predicates:: Tests of equality between any two objects. |
| 66 | * Constants and Mutability:: Whether an object's value can change. | ||
| 62 | @end menu | 67 | @end menu |
| 63 | 68 | ||
| 64 | @node Printed Representation | 69 | @node Printed Representation |
| @@ -2373,3 +2378,43 @@ that for two strings to be equal, they have the same text properties. | |||
| 2373 | @end group | 2378 | @end group |
| 2374 | @end example | 2379 | @end example |
| 2375 | @end defun | 2380 | @end defun |
| 2381 | |||
| 2382 | @node Constants and Mutability | ||
| 2383 | @section Constants and Mutability | ||
| 2384 | @cindex constants | ||
| 2385 | @cindex mutable objects | ||
| 2386 | |||
| 2387 | Some Lisp objects are constant: their values never change. | ||
| 2388 | For example, you can create a new integer by calculating one, but you | ||
| 2389 | cannot modify the value of an existing integer. | ||
| 2390 | |||
| 2391 | Other Lisp objects are mutable: their values can be changed | ||
| 2392 | via destructive operations involving side effects. For example, an | ||
| 2393 | existing marker can be changed by moving the marker to point to | ||
| 2394 | somewhere else. | ||
| 2395 | |||
| 2396 | Although numbers are always constants and markers are always | ||
| 2397 | mutable, some types contain both constant and mutable members. These | ||
| 2398 | types include conses, vectors, and strings. For example, the string | ||
| 2399 | literal @code{"aaa"} yields a constant string, whereas the function | ||
| 2400 | call @code{(make-string 3 ?a)} yields a mutable string that can be | ||
| 2401 | changed via later calls to @code{aset}. | ||
| 2402 | |||
| 2403 | A program should not attempt to modify a constant because the | ||
| 2404 | resulting behavior is undefined: the Lisp interpreter might or might | ||
| 2405 | not detect the error, and if it does not detect the error the | ||
| 2406 | interpreter can behave unpredictably thereafter. Another way to put | ||
| 2407 | this is that mutable objects are safe to change, whereas constants are | ||
| 2408 | not safely mutable: if you try to change a constant your program might | ||
| 2409 | behave as you expect but it might crash or worse. This problem occurs | ||
| 2410 | with types that have both constant and mutable members, and that have | ||
| 2411 | mutators like @code{setcar} and @code{aset} that are valid on mutable | ||
| 2412 | objects but hazardous on constants. | ||
| 2413 | |||
| 2414 | When the same constant occurs multiple times in a program, the Lisp | ||
| 2415 | interpreter might save time or space by reusing existing constants or | ||
| 2416 | constant components. For example, @code{(eq "abc" "abc")} returns | ||
| 2417 | @code{t} if the interpreter creates only one instance of the string | ||
| 2418 | constant @code{"abc"}, and returns @code{nil} if it creates two | ||
| 2419 | instances. Lisp programs should be written so that they work | ||
| 2420 | regardless of whether this optimization is in use. | ||
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 7a3f26e584f..62d60156fbf 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -1249,7 +1249,7 @@ x | |||
| 1249 | 1249 | ||
| 1250 | The @var{array} should be mutable; that is, it should not be a constant, | 1250 | The @var{array} should be mutable; that is, it should not be a constant, |
| 1251 | such as the constants created via quoting or via self-evaluating forms. | 1251 | such as the constants created via quoting or via self-evaluating forms. |
| 1252 | @xref{Self-Evaluating Forms}. | 1252 | @xref{Constants and Mutability}. |
| 1253 | 1253 | ||
| 1254 | If @var{array} is a string and @var{object} is not a character, a | 1254 | If @var{array} is a string and @var{object} is not a character, a |
| 1255 | @code{wrong-type-argument} error results. The function converts a | 1255 | @code{wrong-type-argument} error results. The function converts a |
| @@ -1306,10 +1306,10 @@ same way in Lisp input. | |||
| 1306 | 1306 | ||
| 1307 | A vector, like a string or a number, is considered a constant for | 1307 | A vector, like a string or a number, is considered a constant for |
| 1308 | evaluation: the result of evaluating it is the same vector. This does | 1308 | evaluation: the result of evaluating it is the same vector. This does |
| 1309 | not evaluate or even examine the elements of the vector. Vectors | 1309 | not evaluate or even examine the elements of the vector. |
| 1310 | written with square brackets are constants and should not be modified | 1310 | @xref{Self-Evaluating Forms}. Vectors written with square brackets |
| 1311 | via @code{aset} or other destructive operations. | 1311 | are constants and should not be modified via @code{aset} or other |
| 1312 | @xref{Self-Evaluating Forms}. | 1312 | destructive operations. @xref{Constants and Mutability}. |
| 1313 | 1313 | ||
| 1314 | Here are examples illustrating these principles: | 1314 | Here are examples illustrating these principles: |
| 1315 | 1315 | ||
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 3acbf538dce..a4c9c2549c5 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi | |||
| @@ -384,6 +384,7 @@ usual value is @w{@code{"[ \f\t\n\r\v]+"}}. | |||
| 384 | You can alter the contents of a mutable string via operations | 384 | You can alter the contents of a mutable string via operations |
| 385 | described in this section. However, you should not try to use these | 385 | described in this section. However, you should not try to use these |
| 386 | operations to alter the contents of a constant string. | 386 | operations to alter the contents of a constant string. |
| 387 | @xref{Constants and Mutability}. | ||
| 387 | 388 | ||
| 388 | The most basic way to alter the contents of an existing string is with | 389 | The most basic way to alter the contents of an existing string is with |
| 389 | @code{aset} (@pxref{Array Functions}). @code{(aset @var{string} | 390 | @code{aset} (@pxref{Array Functions}). @code{(aset @var{string} |