aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/objects.texi
diff options
context:
space:
mode:
authorPaul Eggert2020-04-19 12:00:49 -0700
committerPaul Eggert2020-04-19 13:29:57 -0700
commitdca35b31d0a58efdcc698faf90493b96fa8e1406 (patch)
tree7b40c27250607e6010152121d7dc5dc2624532fd /doc/lispref/objects.texi
parent81e7d7f111872c9f2aaf8885db50a22ed746d7b5 (diff)
downloademacs-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.texi45
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
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 never change.
50Others are @dfn{mutable}: their values can be changed via destructive
51operations 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
50syntax of each of the standard types in GNU Emacs Lisp. Details on how 54syntax of each of the standard types in GNU Emacs Lisp. Details on how
51to use these types can be found in later chapters. 55to 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.
2388For example, you can create a new integer by calculating one, but you
2389cannot modify the value of an existing integer.
2390
2391 Other Lisp objects are mutable: their values can be changed
2392via destructive operations involving side effects. For example, an
2393existing marker can be changed by moving the marker to point to
2394somewhere else.
2395
2396 Although numbers are always constants and markers are always
2397mutable, some types contain both constant and mutable members. These
2398types include conses, vectors, and strings. For example, the string
2399literal @code{"aaa"} yields a constant string, whereas the function
2400call @code{(make-string 3 ?a)} yields a mutable string that can be
2401changed via later calls to @code{aset}.
2402
2403 A program should not attempt to modify a constant because the
2404resulting behavior is undefined: the Lisp interpreter might or might
2405not detect the error, and if it does not detect the error the
2406interpreter can behave unpredictably thereafter. Another way to put
2407this is that mutable objects are safe to change, whereas constants are
2408not safely mutable: if you try to change a constant your program might
2409behave as you expect but it might crash or worse. This problem occurs
2410with types that have both constant and mutable members, and that have
2411mutators like @code{setcar} and @code{aset} that are valid on mutable
2412objects but hazardous on constants.
2413
2414 When the same constant occurs multiple times in a program, the Lisp
2415interpreter might save time or space by reusing existing constants or
2416constant components. For example, @code{(eq "abc" "abc")} returns
2417@code{t} if the interpreter creates only one instance of the string
2418constant @code{"abc"}, and returns @code{nil} if it creates two
2419instances. Lisp programs should be written so that they work
2420regardless of whether this optimization is in use.