aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/objects.texi
diff options
context:
space:
mode:
authorGlenn Morris2020-05-23 07:50:29 -0700
committerGlenn Morris2020-05-23 07:50:29 -0700
commitfabcc1ee13f82297d0f5e41f58fb1774840ebd45 (patch)
tree440b7026631d5a26922a7366e28dbb6b3e307347 /doc/lispref/objects.texi
parentf8581bcf6a1942ebd331cae20e32945a3a86a3d1 (diff)
parent4b9fbdb5a713745dfdb13042e33ba2345e6860e1 (diff)
downloademacs-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.texi93
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
46Lisp variables can only take on values of a certain type. 46Lisp 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.
50Others are @dfn{mutable}: their values can be changed via destructive
51operations 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
54syntax of each of the standard types in GNU Emacs Lisp. Details on how 50syntax of each of the standard types in GNU Emacs Lisp. Details on how
55to use these types can be found in later chapters. 51to 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
2392during a single execution of Emacs running well-behaved Lisp code. 2387expression @code{"aaa"} yields a string, but you should not change
2393For example, you can create a new integer by calculating one, but you 2388its contents. And some objects cannot be changed; for example,
2394cannot modify the value of an existing integer. 2389although you can create a new number by calculating one, Lisp provides
2395 2390no operation to change the value of an existing number.
2396 Other Lisp objects are mutable: it is safe to change their values 2391
2397via destructive operations involving side effects. For example, an 2392 Other Lisp objects are @dfn{mutable}: it is safe to change their
2398existing marker can be changed by moving the marker to point to 2393values via destructive operations involving side effects. For
2399somewhere else. 2394example, an existing marker can be changed by moving the marker to
2400 2395point to somewhere else.
2401 Although all numbers are constants and all markers are 2396
2402mutable, some types contain both constant and mutable members. These 2397 Although numbers never change and all markers are mutable,
2403types include conses, vectors, strings, and symbols. For example, the string 2398some types have members some of which are mutable and others not. These
2404literal @code{"aaa"} yields a constant string, whereas the function 2399types include conses, vectors, and strings. For example,
2405call @code{(make-string 3 ?a)} yields a mutable string that can be 2400although @code{"cons"} and @code{(symbol-name 'cons)} both yield
2401strings that should not be changed, @code{(copy-sequence "cons")} and
2402@code{(make-string 3 ?a)} both yield mutable strings that can be
2406changed via later calls to @code{aset}. 2403changed 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
2409that is evaluated. The reverse does not occur: constant objects 2406that is evaluated. For example:
2410should 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))))
2414A program should not attempt to modify other types of constants because the 2411 (setcar x 1.5) ;; The program should not do this.
2415resulting behavior is undefined: the Lisp interpreter might or might 2412 y)
2416not detect the error, and if it does not detect the error the 2413@end example
2417interpreter can behave unpredictably thereafter. Another way to put 2414
2418this is that although mutable objects are safe to change and constant 2415@noindent
2419variables reliably prevent attempts to change them, other constants 2416Although the list @code{(0.5)} was mutable when it was created, it should not
2420are not safely mutable: if a misbehaving program tries to change such a 2417have been changed via @code{setcar} because it given to @code{eval}. The
2421constant then the constant's value might actually change, or the 2418reverse does not occur: an object that should not be changed never
2422program might crash or worse. This problem occurs 2419becomes mutable afterwards.
2423with types that have both constant and mutable members, and that have 2420
2424mutators like @code{setcar} and @code{aset} that are valid on mutable 2421 If a program attempts to change objects that should not be
2425objects but hazardous on constants. 2422changed, the resulting behavior is undefined: the Lisp interpreter
2426 2423might signal an error, or it might crash or behave unpredictably in
2427 When the same constant occurs multiple times in a program, the Lisp 2424other ways.@footnote{This is the behavior specified for languages like
2425Common Lisp and C for constants, and this differs from languages like
2426JavaScript and Python where an interpreter is required to signal an
2427error if a program attempts to change an immutable object. Ideally the Emacs
2428Lisp interpreter will evolve in latter direction.}
2429
2430 When similar constants occur as parts of a program, the Lisp
2428interpreter might save time or space by reusing existing constants or 2431interpreter might save time or space by reusing existing constants or
2429constant components. For example, @code{(eq "abc" "abc")} returns 2432their 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
2431constant @code{"abc"}, and returns @code{nil} if it creates two 2434literal @code{"abc"}, and returns @code{nil} if it creates two
2432instances. Lisp programs should be written so that they work 2435instances. Lisp programs should be written so that they work
2433regardless of whether this optimization is in use. 2436regardless of whether this optimization is in use.