aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2020-05-16 17:17:00 -0700
committerPaul Eggert2020-05-16 18:24:07 -0700
commit6ac2326e5bc4796087910eb429e0cb4384e0e0cf (patch)
treec9a3a32445d1a7dff31829d4efc8f3ed8dcc0c09
parentb4937f64cd97ff6bf93538987c014f8ea8ff9d34 (diff)
downloademacs-6ac2326e5bc4796087910eb429e0cb4384e0e0cf.tar.gz
emacs-6ac2326e5bc4796087910eb429e0cb4384e0e0cf.zip
Don’t use “constant” for values you shouldn’t change
Inspired by patch proposed by Dmitry Gutov (Bug#40671#393) and by further comments by him and by Michael Heerdegen in the same bug report. * doc/lispintro/emacs-lisp-intro.texi (setcar): Don’t push mutability here. * doc/lispref/eval.texi (Self-Evaluating Forms, Quoting) (Backquote): * doc/lispref/lists.texi (Modifying Lists): * doc/lispref/objects.texi (Lisp Data Types, Mutability): * doc/lispref/sequences.texi (Array Functions, Vectors): * doc/lispref/strings.texi (String Basics, Modifying Strings): Don’t use the word “constant” to describe all values that a program should not change. * doc/lispref/objects.texi (Mutability): Rename from “Constants and Mutability”. All uses changed. In a footnote, contrast the Emacs behavior with that of Common Lisp, Python, etc. for clarity, and say the goal is to be nicer.
-rw-r--r--doc/lispintro/emacs-lisp-intro.texi5
-rw-r--r--doc/lispref/elisp.texi2
-rw-r--r--doc/lispref/eval.texi21
-rw-r--r--doc/lispref/lists.texi16
-rw-r--r--doc/lispref/objects.texi98
-rw-r--r--doc/lispref/sequences.texi25
-rw-r--r--doc/lispref/strings.texi11
7 files changed, 87 insertions, 91 deletions
diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi
index ea16d9ef155..46462162ca0 100644
--- a/doc/lispintro/emacs-lisp-intro.texi
+++ b/doc/lispintro/emacs-lisp-intro.texi
@@ -7317,8 +7317,6 @@ which leave the original list as it was. One way to find out how this
7317works is to experiment. We will start with the @code{setcar} function. 7317works is to experiment. We will start with the @code{setcar} function.
7318 7318
7319@need 1200 7319@need 1200
7320@cindex constant lists
7321@cindex mutable lists
7322First, we can make a list and then set the value of a variable to the 7320First, we can make a list and then set the value of a variable to the
7323list, using the @code{setq} special form. Because we intend to use 7321list, using the @code{setq} special form. Because we intend to use
7324@code{setcar} to change the list, this @code{setq} should not use the 7322@code{setcar} to change the list, this @code{setq} should not use the
@@ -7327,8 +7325,7 @@ a list that is part of the program and bad things could happen if we
7327tried to change part of the program while running it. Generally 7325tried to change part of the program while running it. Generally
7328speaking an Emacs Lisp program's components should be constant (or 7326speaking an Emacs Lisp program's components should be constant (or
7329unchanged) while the program is running. So we instead construct an 7327unchanged) while the program is running. So we instead construct an
7330animal list that is @dfn{mutable} (or changeable) by using the 7328animal list by using the @code{list} function, as follows:
7331@code{list} function, as follows:
7332 7329
7333@smallexample 7330@smallexample
7334(setq animals (list 'antelope 'giraffe 'lion 'tiger)) 7331(setq animals (list 'antelope 'giraffe 'lion 'tiger))
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index bba1b63115f..9a6796790c4 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -297,7 +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* Mutability:: Some objects should not be modified.
301 301
302Programming Types 302Programming Types
303 303
diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi
index baddce4d9c9..39f342a798b 100644
--- a/doc/lispref/eval.texi
+++ b/doc/lispref/eval.texi
@@ -158,11 +158,11 @@ contents unchanged.
158@end group 158@end group
159@end example 159@end example
160 160
161 A self-evaluating form yields constant conses, vectors and strings, and you 161 A self-evaluating form yields a value that becomes part of the program,
162should not attempt to modify their contents via @code{setcar}, @code{aset} or 162and you should not try to modify it via @code{setcar}, @code{aset} or
163similar operations. The Lisp interpreter might unify the constants 163similar operations. The Lisp interpreter might unify the constants
164yielded by your program's self-evaluating forms, so that these 164yielded by your program's self-evaluating forms, so that these
165constants might share structure. @xref{Constants and Mutability}. 165constants might share structure. @xref{Mutability}.
166 166
167 It is common to write numbers, characters, strings, and even vectors 167 It is common to write numbers, characters, strings, and even vectors
168in Lisp code, taking advantage of the fact that they self-evaluate. 168in Lisp code, taking advantage of the fact that they self-evaluate.
@@ -564,8 +564,8 @@ and vectors.)
564 564
565@defspec quote object 565@defspec quote object
566This special form returns @var{object}, without evaluating it. 566This special form returns @var{object}, without evaluating it.
567The returned value is a constant, and should not be modified. 567The returned value might be shared and should not be modified.
568@xref{Constants and Mutability}. 568@xref{Self-Evaluating Forms}.
569@end defspec 569@end defspec
570 570
571@cindex @samp{'} for quoting 571@cindex @samp{'} for quoting
@@ -608,9 +608,9 @@ Here are some examples of expressions that use @code{quote}:
608 608
609 Although the expressions @code{(list '+ 1 2)} and @code{'(+ 1 2)} 609 Although the expressions @code{(list '+ 1 2)} and @code{'(+ 1 2)}
610both yield lists equal to @code{(+ 1 2)}, the former yields a 610both yield lists equal to @code{(+ 1 2)}, the former yields a
611freshly-minted mutable list whereas the latter yields a constant list 611freshly-minted mutable list whereas the latter yields a list
612built from conses that may be shared with other constants. 612built from conses that might be shared and should not be modified.
613@xref{Constants and Mutability}. 613@xref{Self-Evaluating Forms}.
614 614
615 Other quoting constructs include @code{function} (@pxref{Anonymous 615 Other quoting constructs include @code{function} (@pxref{Anonymous
616Functions}), which causes an anonymous lambda expression written in Lisp 616Functions}), which causes an anonymous lambda expression written in Lisp
@@ -710,8 +710,9 @@ Here are some examples:
710@end example 710@end example
711 711
712If a subexpression of a backquote construct has no substitutions or 712If a subexpression of a backquote construct has no substitutions or
713splices, it acts like @code{quote} in that it yields constant conses, 713splices, it acts like @code{quote} in that it yields conses,
714vectors and strings that should not be modified. 714vectors and strings that might be shared and should not be modified.
715@xref{Self-Evaluating Forms}.
715 716
716@node Eval 717@node Eval
717@section Eval 718@section Eval
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi
index fcaf4386b15..ae793d5e15e 100644
--- a/doc/lispref/lists.texi
+++ b/doc/lispref/lists.texi
@@ -873,8 +873,8 @@ primitives @code{setcar} and @code{setcdr}. These are destructive
873operations because they change existing list structure. 873operations because they change existing list structure.
874Destructive operations should be applied only to mutable lists, 874Destructive operations should be applied only to mutable lists,
875that is, lists constructed via @code{cons}, @code{list} or similar 875that is, lists constructed via @code{cons}, @code{list} or similar
876operations. Lists created by quoting are constants and should not be 876operations. Lists created by quoting are part of the program and
877changed by destructive operations. @xref{Constants and Mutability}. 877should not be changed by destructive operations. @xref{Mutability}.
878 878
879@cindex CL note---@code{rplaca} vs @code{setcar} 879@cindex CL note---@code{rplaca} vs @code{setcar}
880@quotation 880@quotation
@@ -911,7 +911,7 @@ value @var{object}. For example:
911 911
912@example 912@example
913@group 913@group
914(setq x (list 1 2)) ; @r{Create a mutable list.} 914(setq x (list 1 2))
915 @result{} (1 2) 915 @result{} (1 2)
916@end group 916@end group
917@group 917@group
@@ -931,7 +931,7 @@ these lists. Here is an example:
931 931
932@example 932@example
933@group 933@group
934;; @r{Create two mutable lists that are partly shared.} 934;; @r{Create two lists that are partly shared.}
935(setq x1 (list 'a 'b 'c)) 935(setq x1 (list 'a 'b 'c))
936 @result{} (a b c) 936 @result{} (a b c)
937(setq x2 (cons 'z (cdr x1))) 937(setq x2 (cons 'z (cdr x1)))
@@ -1022,11 +1022,11 @@ reached via the @sc{cdr}.
1022 1022
1023@example 1023@example
1024@group 1024@group
1025(setq x (list 1 2 3)) ; @r{Create a mutable list.} 1025(setq x (list 1 2 3))
1026 @result{} (1 2 3) 1026 @result{} (1 2 3)
1027@end group 1027@end group
1028@group 1028@group
1029(setcdr x '(4)) ; @r{Modify the list's tail to be a constant list.} 1029(setcdr x '(4))
1030 @result{} (4) 1030 @result{} (4)
1031@end group 1031@end group
1032@group 1032@group
@@ -1135,11 +1135,11 @@ Unlike @code{append} (@pxref{Building Lists}), the @var{lists} are
1135 1135
1136@example 1136@example
1137@group 1137@group
1138(setq x (list 1 2 3)) ; @r{Create a mutable list.} 1138(setq x (list 1 2 3))
1139 @result{} (1 2 3) 1139 @result{} (1 2 3)
1140@end group 1140@end group
1141@group 1141@group
1142(nconc x '(4 5)) ; @r{Modify the list's tail to be a constant list.} 1142(nconc x '(4 5))
1143 @result{} (1 2 3 4 5) 1143 @result{} (1 2 3 4 5)
1144@end group 1144@end group
1145@group 1145@group
diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi
index 1d5b2c690fe..136213ad661 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
@@ -2379,51 +2375,59 @@ that for two strings to be equal, they have the same text properties.
2379@end example 2375@end example
2380@end defun 2376@end defun
2381 2377
2382@node Constants and Mutability 2378@node Mutability
2383@section Constants and Mutability 2379@section Mutability
2384@cindex constants
2385@cindex mutable objects 2380@cindex mutable objects
2386 2381
2387 Some Lisp objects are constant: their values should never change 2382 Some Lisp objects should never change. For example, the Lisp
2388during a single execution of Emacs running well-behaved Lisp code. 2383expression @code{"aaa"} yields a string, but you should not change
2389For example, you can create a new integer by calculating one, but you 2384its contents. Indeed, some objects cannot be changed; for example,
2390cannot modify the value of an existing integer. 2385although you can create a new number by calculating one, Lisp provides
2391 2386no operation to change the value of an existing number.
2392 Other Lisp objects are mutable: it is safe to change their values 2387
2393via destructive operations involving side effects. For example, an 2388 Other Lisp objects are @dfn{mutable}: it is safe to change their
2394existing marker can be changed by moving the marker to point to 2389values via destructive operations involving side effects. For
2395somewhere else. 2390example, an existing marker can be changed by moving the marker to
2396 2391point to somewhere else.
2397 Although all numbers are constants and all markers are 2392
2398mutable, some types contain both constant and mutable members. These 2393 Although numbers never change and all markers are mutable,
2399types include conses, vectors, strings, and symbols. For example, the string 2394some types have members some of which are mutable and others not. These
2400literal @code{"aaa"} yields a constant string, whereas the function 2395types include conses, vectors, and strings. For example,
2401call @code{(make-string 3 ?a)} yields a mutable string that can be 2396although @code{"aaa"} yields a string that should not be changed,
2402changed via later calls to @code{aset}. 2397@code{(make-string 3 ?a)} yields a mutable string that can be
2403 2398changed via later calls to @code{aset}. Another example:
2404 A mutable object can become constant if it is part of an expression 2399@code{(symbol-name 'cons)} yields a string @code{"cons"} that should
2405that is evaluated. The reverse does not occur: constant objects 2400not be changed.
2406should stay constant. 2401
2407 2402 A mutable object stops being mutable if it is part of an expression
2408 Trying to modify a constant variable signals an error 2403that is evaluated. For example:
2409(@pxref{Constant Variables}). 2404
2410A program should not attempt to modify other types of constants because the 2405@example
2411resulting behavior is undefined: the Lisp interpreter might or might 2406(let* ((x (list 0.5))
2412not detect the error, and if it does not detect the error the 2407 (y (eval (list 'quote x))))
2413interpreter can behave unpredictably thereafter. Another way to put 2408 (setcar x 1.5) ;; The program should not do this.
2414this is that although mutable objects are safe to change and constant 2409 y)
2415variables reliably prevent attempts to change them, other constants 2410@end example
2416are not safely mutable: if a misbehaving program tries to change such a 2411
2417constant then the constant's value might actually change, or the 2412@noindent
2418program might crash or worse. This problem occurs 2413Although the list @code{(0.5)} was mutable when it was created, it should not
2419with types that have both constant and mutable members, and that have 2414have been changed via @code{setcar} because it given to @code{eval}. The
2420mutators like @code{setcar} and @code{aset} that are valid on mutable 2415reverse does not occur: an object that should not be changed never
2421objects but hazardous on constants. 2416becomes mutable afterwards.
2422 2417
2423 When the same constant occurs multiple times in a program, the Lisp 2418 If a program attempts to change objects that should not be
2419changed, the resulting behavior is undefined: the Lisp interpreter
2420might signal an error, or it might crash or behave unpredictably in
2421other ways.@footnote{This is the behavior specified for languages like
2422Common Lisp and C, and it differs from the behavior of languages like
2423JavaScript and Python where an interpreter is required to signal an
2424error if a program attempts to change a constant. Ideally the Emacs
2425Lisp interpreter will evolve in latter direction.}
2426
2427 When similar constants occur as parts of a program, the Lisp
2424interpreter might save time or space by reusing existing constants or 2428interpreter might save time or space by reusing existing constants or
2425constant components. For example, @code{(eq "abc" "abc")} returns 2429their components. For example, @code{(eq "abc" "abc")} returns
2426@code{t} if the interpreter creates only one instance of the string 2430@code{t} if the interpreter creates only one instance of the string
2427constant @code{"abc"}, and returns @code{nil} if it creates two 2431literal @code{"abc"}, and returns @code{nil} if it creates two
2428instances. Lisp programs should be written so that they work 2432instances. Lisp programs should be written so that they work
2429regardless of whether this optimization is in use. 2433regardless of whether this optimization is in use.
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 1cb0d05cc7b..91c3049f875 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -183,11 +183,11 @@ for other ways to copy sequences.
183 183
184@example 184@example
185@group 185@group
186(setq bar (list 1 2)) ; @r{Create a mutable list.} 186(setq bar (list 1 2))
187 @result{} (1 2) 187 @result{} (1 2)
188@end group 188@end group
189@group 189@group
190(setq x (vector 'foo bar)) ; @r{Create a mutable vector.} 190(setq x (vector 'foo bar))
191 @result{} [foo (1 2)] 191 @result{} [foo (1 2)]
192@end group 192@end group
193@group 193@group
@@ -278,7 +278,7 @@ Unlike @code{reverse} the original @var{sequence} may be modified.
278 278
279@example 279@example
280@group 280@group
281(setq x (list 'a 'b 'c)) ; @r{Create a mutable list.} 281(setq x (list 'a 'b 'c))
282 @result{} (a b c) 282 @result{} (a b c)
283@end group 283@end group
284@group 284@group
@@ -320,7 +320,7 @@ presented graphically:
320 For the vector, it is even simpler because you don't need setq: 320 For the vector, it is even simpler because you don't need setq:
321 321
322@example 322@example
323(setq x (copy-sequence [1 2 3 4])) ; @r{Create a mutable vector.} 323(setq x (copy-sequence [1 2 3 4]))
324 @result{} [1 2 3 4] 324 @result{} [1 2 3 4]
325(nreverse x) 325(nreverse x)
326 @result{} [4 3 2 1] 326 @result{} [4 3 2 1]
@@ -331,6 +331,7 @@ x
331Note that unlike @code{reverse}, this function doesn't work with strings. 331Note that unlike @code{reverse}, this function doesn't work with strings.
332Although you can alter string data by using @code{aset}, it is strongly 332Although you can alter string data by using @code{aset}, it is strongly
333encouraged to treat strings as immutable even when they are mutable. 333encouraged to treat strings as immutable even when they are mutable.
334@xref{Mutability}.
334 335
335@end defun 336@end defun
336 337
@@ -374,7 +375,7 @@ appears in a different position in the list due to the change of
374 375
375@example 376@example
376@group 377@group
377(setq nums (list 1 3 2 6 5 4 0)) ; @r{Create a mutable list.} 378(setq nums (list 1 3 2 6 5 4 0))
378 @result{} (1 3 2 6 5 4 0) 379 @result{} (1 3 2 6 5 4 0)
379@end group 380@end group
380@group 381@group
@@ -1228,7 +1229,7 @@ This function sets the @var{index}th element of @var{array} to be
1228 1229
1229@example 1230@example
1230@group 1231@group
1231(setq w (vector 'foo 'bar 'baz)) ; @r{Create a mutable vector.} 1232(setq w (vector 'foo 'bar 'baz))
1232 @result{} [foo bar baz] 1233 @result{} [foo bar baz]
1233(aset w 0 'fu) 1234(aset w 0 'fu)
1234 @result{} fu 1235 @result{} fu
@@ -1237,7 +1238,7 @@ w
1237@end group 1238@end group
1238 1239
1239@group 1240@group
1240;; @r{@code{copy-sequence} creates a mutable string.} 1241;; @r{@code{copy-sequence} copies the string to be modified later.}
1241(setq x (copy-sequence "asdfasfd")) 1242(setq x (copy-sequence "asdfasfd"))
1242 @result{} "asdfasfd" 1243 @result{} "asdfasfd"
1243(aset x 3 ?Z) 1244(aset x 3 ?Z)
@@ -1247,9 +1248,7 @@ x
1247@end group 1248@end group
1248@end example 1249@end example
1249 1250
1250The @var{array} should be mutable; that is, it should not be a constant, 1251The @var{array} should be mutable. @xref{Mutability}.
1251such as the constants created via quoting or via self-evaluating forms.
1252@xref{Constants and Mutability}.
1253 1252
1254If @var{array} is a string and @var{object} is not a character, a 1253If @var{array} is a string and @var{object} is not a character, a
1255@code{wrong-type-argument} error results. The function converts a 1254@code{wrong-type-argument} error results. The function converts a
@@ -1262,7 +1261,6 @@ each element of @var{array} is @var{object}. It returns @var{array}.
1262 1261
1263@example 1262@example
1264@group 1263@group
1265;; @r{Create a mutable vector and then fill it with zeros.}
1266(setq a (copy-sequence [a b c d e f g])) 1264(setq a (copy-sequence [a b c d e f g]))
1267 @result{} [a b c d e f g] 1265 @result{} [a b c d e f g]
1268(fillarray a 0) 1266(fillarray a 0)
@@ -1271,7 +1269,6 @@ a
1271 @result{} [0 0 0 0 0 0 0] 1269 @result{} [0 0 0 0 0 0 0]
1272@end group 1270@end group
1273@group 1271@group
1274;; @r{Create a mutable string and then fill it with "-".}
1275(setq s (copy-sequence "When in the course")) 1272(setq s (copy-sequence "When in the course"))
1276 @result{} "When in the course" 1273 @result{} "When in the course"
1277(fillarray s ?-) 1274(fillarray s ?-)
@@ -1310,8 +1307,8 @@ same way in Lisp input.
1310evaluation: the result of evaluating it is the same vector. This does 1307evaluation: the result of evaluating it is the same vector. This does
1311not evaluate or even examine the elements of the vector. 1308not evaluate or even examine the elements of the vector.
1312@xref{Self-Evaluating Forms}. Vectors written with square brackets 1309@xref{Self-Evaluating Forms}. Vectors written with square brackets
1313are constants and should not be modified via @code{aset} or other 1310should not be modified via @code{aset} or other destructive
1314destructive operations. @xref{Constants and Mutability}. 1311operations. @xref{Mutability}.
1315 1312
1316 Here are examples illustrating these principles: 1313 Here are examples illustrating these principles:
1317 1314
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index a4c9c2549c5..70c3b3cf4be 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -49,10 +49,9 @@ by a distinguished character code.
49 49
50 Since strings are arrays, and therefore sequences as well, you can 50 Since strings are arrays, and therefore sequences as well, you can
51operate on them with the general array and sequence functions documented 51operate on them with the general array and sequence functions documented
52in @ref{Sequences Arrays Vectors}. For example, you can access or 52in @ref{Sequences Arrays Vectors}. For example, you can access
53change individual characters in a string using the functions @code{aref} 53individual characters in a string using the function @code{aref}
54and @code{aset} (@pxref{Array Functions}). However, you should not 54(@pxref{Array Functions}).
55try to change the contents of constant strings (@pxref{Modifying Strings}).
56 55
57 There are two text representations for non-@acronym{ASCII} 56 There are two text representations for non-@acronym{ASCII}
58characters in Emacs strings (and in buffers): unibyte and multibyte. 57characters in Emacs strings (and in buffers): unibyte and multibyte.
@@ -382,9 +381,7 @@ usual value is @w{@code{"[ \f\t\n\r\v]+"}}.
382@cindex string modification 381@cindex string modification
383 382
384 You can alter the contents of a mutable string via operations 383 You can alter the contents of a mutable string via operations
385described in this section. However, you should not try to use these 384described in this section. @xref{Mutability}.
386operations to alter the contents of a constant string.
387@xref{Constants and Mutability}.
388 385
389 The most basic way to alter the contents of an existing string is with 386 The most basic way to alter the contents of an existing string is with
390@code{aset} (@pxref{Array Functions}). @code{(aset @var{string} 387@code{aset} (@pxref{Array Functions}). @code{(aset @var{string}