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/lispref/objects.texi | |
| 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/lispref/objects.texi')
| -rw-r--r-- | doc/lispref/objects.texi | 45 |
1 files changed, 45 insertions, 0 deletions
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. | ||