diff options
| author | Glenn Morris | 2020-05-23 07:50:29 -0700 |
|---|---|---|
| committer | Glenn Morris | 2020-05-23 07:50:29 -0700 |
| commit | fabcc1ee13f82297d0f5e41f58fb1774840ebd45 (patch) | |
| tree | 440b7026631d5a26922a7366e28dbb6b3e307347 /doc/lispref/objects.texi | |
| parent | f8581bcf6a1942ebd331cae20e32945a3a86a3d1 (diff) | |
| parent | 4b9fbdb5a713745dfdb13042e33ba2345e6860e1 (diff) | |
| download | emacs-fabcc1ee13f82297d0f5e41f58fb1774840ebd45.tar.gz emacs-fabcc1ee13f82297d0f5e41f58fb1774840ebd45.zip | |
Merge from origin/emacs-27
4b9fbdb5a7 ; Update TODO item about ligature support
03d44acfdd * doc/lispref/control.texi (Processing of Errors): Improve...
b48ab743a8 Minor fixups for mutability doc
6ac2326e5b Don’t use “constant” for values you shouldn’t change
Diffstat (limited to 'doc/lispref/objects.texi')
| -rw-r--r-- | doc/lispref/objects.texi | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi index cd037d663da..83066744121 100644 --- a/doc/lispref/objects.texi +++ b/doc/lispref/objects.texi | |||
| @@ -46,10 +46,6 @@ 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 should never change. | ||
| 50 | Others are @dfn{mutable}: their values can be changed via destructive | ||
| 51 | operations that involve side effects. | ||
| 52 | |||
| 53 | This chapter describes the purpose, printed representation, and read | 49 | This chapter describes the purpose, printed representation, and read |
| 54 | syntax of each of the standard types in GNU Emacs Lisp. Details on how | 50 | syntax of each of the standard types in GNU Emacs Lisp. Details on how |
| 55 | to use these types can be found in later chapters. | 51 | to use these types can be found in later chapters. |
| @@ -63,7 +59,7 @@ to use these types can be found in later chapters. | |||
| 63 | * Circular Objects:: Read syntax for circular structure. | 59 | * Circular Objects:: Read syntax for circular structure. |
| 64 | * Type Predicates:: Tests related to types. | 60 | * Type Predicates:: Tests related to types. |
| 65 | * Equality Predicates:: Tests of equality between any two objects. | 61 | * Equality Predicates:: Tests of equality between any two objects. |
| 66 | * Constants and Mutability:: Whether an object's value can change. | 62 | * Mutability:: Some objects should not be modified. |
| 67 | @end menu | 63 | @end menu |
| 68 | 64 | ||
| 69 | @node Printed Representation | 65 | @node Printed Representation |
| @@ -2383,51 +2379,58 @@ that for two strings to be equal, they have the same text properties. | |||
| 2383 | @end example | 2379 | @end example |
| 2384 | @end defun | 2380 | @end defun |
| 2385 | 2381 | ||
| 2386 | @node Constants and Mutability | 2382 | @node Mutability |
| 2387 | @section Constants and Mutability | 2383 | @section Mutability |
| 2388 | @cindex constants | ||
| 2389 | @cindex mutable objects | 2384 | @cindex mutable objects |
| 2390 | 2385 | ||
| 2391 | Some Lisp objects are constant: their values should never change | 2386 | Some Lisp objects should never change. For example, the Lisp |
| 2392 | during a single execution of Emacs running well-behaved Lisp code. | 2387 | expression @code{"aaa"} yields a string, but you should not change |
| 2393 | For example, you can create a new integer by calculating one, but you | 2388 | its contents. And some objects cannot be changed; for example, |
| 2394 | cannot modify the value of an existing integer. | 2389 | although you can create a new number by calculating one, Lisp provides |
| 2395 | 2390 | no operation to change the value of an existing number. | |
| 2396 | Other Lisp objects are mutable: it is safe to change their values | 2391 | |
| 2397 | via destructive operations involving side effects. For example, an | 2392 | Other Lisp objects are @dfn{mutable}: it is safe to change their |
| 2398 | existing marker can be changed by moving the marker to point to | 2393 | values via destructive operations involving side effects. For |
| 2399 | somewhere else. | 2394 | example, an existing marker can be changed by moving the marker to |
| 2400 | 2395 | point to somewhere else. | |
| 2401 | Although all numbers are constants and all markers are | 2396 | |
| 2402 | mutable, some types contain both constant and mutable members. These | 2397 | Although numbers never change and all markers are mutable, |
| 2403 | types include conses, vectors, strings, and symbols. For example, the string | 2398 | some types have members some of which are mutable and others not. These |
| 2404 | literal @code{"aaa"} yields a constant string, whereas the function | 2399 | types include conses, vectors, and strings. For example, |
| 2405 | call @code{(make-string 3 ?a)} yields a mutable string that can be | 2400 | although @code{"cons"} and @code{(symbol-name 'cons)} both yield |
| 2401 | strings that should not be changed, @code{(copy-sequence "cons")} and | ||
| 2402 | @code{(make-string 3 ?a)} both yield mutable strings that can be | ||
| 2406 | changed via later calls to @code{aset}. | 2403 | changed via later calls to @code{aset}. |
| 2407 | 2404 | ||
| 2408 | A mutable object can become constant if it is part of an expression | 2405 | A mutable object stops being mutable if it is part of an expression |
| 2409 | that is evaluated. The reverse does not occur: constant objects | 2406 | that is evaluated. For example: |
| 2410 | should stay constant. | 2407 | |
| 2411 | 2408 | @example | |
| 2412 | Trying to modify a constant variable signals an error | 2409 | (let* ((x (list 0.5)) |
| 2413 | (@pxref{Constant Variables}). | 2410 | (y (eval (list 'quote x)))) |
| 2414 | A program should not attempt to modify other types of constants because the | 2411 | (setcar x 1.5) ;; The program should not do this. |
| 2415 | resulting behavior is undefined: the Lisp interpreter might or might | 2412 | y) |
| 2416 | not detect the error, and if it does not detect the error the | 2413 | @end example |
| 2417 | interpreter can behave unpredictably thereafter. Another way to put | 2414 | |
| 2418 | this is that although mutable objects are safe to change and constant | 2415 | @noindent |
| 2419 | variables reliably prevent attempts to change them, other constants | 2416 | Although the list @code{(0.5)} was mutable when it was created, it should not |
| 2420 | are not safely mutable: if a misbehaving program tries to change such a | 2417 | have been changed via @code{setcar} because it given to @code{eval}. The |
| 2421 | constant then the constant's value might actually change, or the | 2418 | reverse does not occur: an object that should not be changed never |
| 2422 | program might crash or worse. This problem occurs | 2419 | becomes mutable afterwards. |
| 2423 | with types that have both constant and mutable members, and that have | 2420 | |
| 2424 | mutators like @code{setcar} and @code{aset} that are valid on mutable | 2421 | If a program attempts to change objects that should not be |
| 2425 | objects but hazardous on constants. | 2422 | changed, the resulting behavior is undefined: the Lisp interpreter |
| 2426 | 2423 | might signal an error, or it might crash or behave unpredictably in | |
| 2427 | When the same constant occurs multiple times in a program, the Lisp | 2424 | other ways.@footnote{This is the behavior specified for languages like |
| 2425 | Common Lisp and C for constants, and this differs from languages like | ||
| 2426 | JavaScript and Python where an interpreter is required to signal an | ||
| 2427 | error if a program attempts to change an immutable object. Ideally the Emacs | ||
| 2428 | Lisp interpreter will evolve in latter direction.} | ||
| 2429 | |||
| 2430 | When similar constants occur as parts of a program, the Lisp | ||
| 2428 | interpreter might save time or space by reusing existing constants or | 2431 | interpreter might save time or space by reusing existing constants or |
| 2429 | constant components. For example, @code{(eq "abc" "abc")} returns | 2432 | their components. For example, @code{(eq "abc" "abc")} returns |
| 2430 | @code{t} if the interpreter creates only one instance of the string | 2433 | @code{t} if the interpreter creates only one instance of the string |
| 2431 | constant @code{"abc"}, and returns @code{nil} if it creates two | 2434 | literal @code{"abc"}, and returns @code{nil} if it creates two |
| 2432 | instances. Lisp programs should be written so that they work | 2435 | instances. Lisp programs should be written so that they work |
| 2433 | regardless of whether this optimization is in use. | 2436 | regardless of whether this optimization is in use. |