aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2023-10-21 17:39:25 +0200
committerMattias EngdegÄrd2023-10-23 14:48:42 +0200
commit7e4a4b762e73ec03be18a149b737447facff3612 (patch)
tree3ea261b602e622fe31a3f1e0f8634c99cca5a6fd
parent48e7f5493e4ffd31cb705adf982485c3b30fbbac (diff)
downloademacs-7e4a4b762e73ec03be18a149b737447facff3612.tar.gz
emacs-7e4a4b762e73ec03be18a149b737447facff3612.zip
Describe lexical binding before dynamic
* doc/lispref/variables.texi (Variable Scoping) (Lexical Binding, Dynamic Binding): Alter the presentation order from the point of view that lexical binding is the standard discipline (if not always the default) and dynamic binding an alternative, which corresponds better to Elisp today. Modernise parts of the text. * doc/lispref/elisp.texi (Top): Update menu.
-rw-r--r--doc/lispref/elisp.texi4
-rw-r--r--doc/lispref/variables.texi254
2 files changed, 127 insertions, 131 deletions
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index 72441c8d442..8bf8358153c 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -534,9 +534,9 @@ Variables
534 534
535Scoping Rules for Variable Bindings 535Scoping Rules for Variable Bindings
536 536
537* Dynamic Binding:: The default for binding local variables in Emacs. 537* Lexical Binding:: The standard type of local variable binding.
538* Dynamic Binding:: A different type of local variable binding.
538* Dynamic Binding Tips:: Avoiding problems with dynamic binding. 539* Dynamic Binding Tips:: Avoiding problems with dynamic binding.
539* Lexical Binding:: A different type of local variable binding.
540* Using Lexical Binding:: How to enable lexical binding. 540* Using Lexical Binding:: How to enable lexical binding.
541* Converting to Lexical Binding:: Convert existing code to lexical binding. 541* Converting to Lexical Binding:: Convert existing code to lexical binding.
542 542
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi
index 779f6233735..f793a83a703 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -976,50 +976,152 @@ Variables}). This section describes exactly what this means.
976binding can be accessed. @dfn{Extent} refers to @emph{when}, as the 976binding can be accessed. @dfn{Extent} refers to @emph{when}, as the
977program is executing, the binding exists. 977program is executing, the binding exists.
978 978
979@cindex dynamic binding
980@cindex dynamic scope
981@cindex dynamic extent
982 By default, the local bindings that Emacs creates are @dfn{dynamic
983bindings}. Such a binding has @dfn{dynamic scope}, meaning that any
984part of the program can potentially access the variable binding. It
985also has @dfn{dynamic extent}, meaning that the binding lasts only
986while the binding construct (such as the body of a @code{let} form) is
987being executed.
988
989@cindex lexical binding 979@cindex lexical binding
990@cindex lexical scope 980@cindex lexical scope
991@cindex indefinite extent 981@cindex indefinite extent
992 Emacs can optionally create @dfn{lexical bindings}. A lexical 982 For historical reasons, there are two dialects of Emacs Lisp,
993binding has @dfn{lexical scope}, meaning that any reference to the 983selected via the @code{lexical-binding} buffer-local variable.
994variable must be located textually within the binding 984In the modern Emacs Lisp dialect, local bindings are lexical by default.
985A @dfn{lexical binding} has @dfn{lexical scope}, meaning that any
986reference to the variable must be located textually within the binding
995construct@footnote{With some exceptions; for instance, a lexical 987construct@footnote{With some exceptions; for instance, a lexical
996binding can also be accessed from the Lisp debugger.}. It also has 988binding can also be accessed from the Lisp debugger.}. It also has
997@dfn{indefinite extent}, meaning that under some circumstances the 989@dfn{indefinite extent}, meaning that under some circumstances the
998binding can live on even after the binding construct has finished 990binding can live on even after the binding construct has finished
999executing, by means of special objects called @dfn{closures}. 991executing, by means of objects called @dfn{closures}.
1000 992
1001 The dynamic binding was (and still is) the default in Emacs for many 993@cindex dynamic binding
1002years, but lately Emacs is moving towards using lexical binding in 994@cindex dynamic scope
1003more and more places, with the goal of eventually making that the 995@cindex dynamic extent
1004default. 996 Local bindings can also be dynamic, which they always are in the
997old Emacs Lisp dialect and optionally in the modern dialect.
998A @dfn{dynamic binding} has @dfn{dynamic scope}, meaning that any
999part of the program can potentially access the variable binding. It
1000also has @dfn{dynamic extent}, meaning that the binding lasts only
1001while the binding construct (such as the body of a @code{let} form) is
1002being executed.
1005 1003
1006 The following subsections describe dynamic binding and lexical 1004 The old dynamic-only Emacs Lisp dialect is still the default in code
1005loaded or evaluated from Lisp files that lack a dialect declaration.
1006Eventually the modern dialect will be made the default.
1007All Lisp files should declare the dialect used to ensure that they
1008keep working correctly in the future.
1009
1010 The following subsections describe lexical binding and dynamic
1007binding in greater detail, and how to enable lexical binding in Emacs 1011binding in greater detail, and how to enable lexical binding in Emacs
1008Lisp programs. 1012Lisp programs.
1009 1013
1010@menu 1014@menu
1011* Dynamic Binding:: The default for binding local variables in Emacs. 1015* Lexical Binding:: The standard type of local variable binding.
1016* Dynamic Binding:: A different type of local variable binding.
1012* Dynamic Binding Tips:: Avoiding problems with dynamic binding. 1017* Dynamic Binding Tips:: Avoiding problems with dynamic binding.
1013* Lexical Binding:: A different type of local variable binding.
1014* Using Lexical Binding:: How to enable lexical binding. 1018* Using Lexical Binding:: How to enable lexical binding.
1015* Converting to Lexical Binding:: Convert existing code to lexical binding. 1019* Converting to Lexical Binding:: Convert existing code to lexical binding.
1016@end menu 1020@end menu
1017 1021
1022@node Lexical Binding
1023@subsection Lexical Binding
1024
1025Lexical binding is only available in the modern Emacs Lisp dialect.
1026(@xref{Using Lexical Binding}.)
1027A lexically-bound variable has @dfn{lexical scope}, meaning that any
1028reference to the variable must be located textually within the binding
1029construct. Here is an example
1030
1031@example
1032@group
1033(let ((x 1)) ; @r{@code{x} is lexically bound.}
1034 (+ x 3))
1035 @result{} 4
1036
1037(defun getx ()
1038 x) ; @r{@code{x} is used free in this function.}
1039
1040(let ((x 1)) ; @r{@code{x} is lexically bound.}
1041 (getx))
1042@error{} Symbol's value as variable is void: x
1043@end group
1044@end example
1045
1046@noindent
1047Here, the variable @code{x} has no global value. When it is lexically
1048bound within a @code{let} form, it can be used in the textual confines
1049of that @code{let} form. But it can @emph{not} be used from within a
1050@code{getx} function called from the @code{let} form, since the
1051function definition of @code{getx} occurs outside the @code{let} form
1052itself.
1053
1054@cindex lexical environment
1055 Here is how lexical binding works. Each binding construct defines a
1056@dfn{lexical environment}, specifying the variables that are bound
1057within the construct and their local values. When the Lisp evaluator
1058wants the current value of a variable, it looks first in the lexical
1059environment; if the variable is not specified in there, it looks in
1060the symbol's value cell, where the dynamic value is stored.
1061
1062@cindex closures, example of using
1063 Lexical bindings have indefinite extent. Even after a binding
1064construct has finished executing, its lexical environment can be
1065``kept around'' in Lisp objects called @dfn{closures}. A closure is
1066created when you define a named or anonymous function with lexical
1067binding enabled. @xref{Closures}, for details.
1068
1069 When a closure is called as a function, any lexical variable
1070references within its definition use the retained lexical environment.
1071Here is an example:
1072
1073@example
1074(defvar my-ticker nil) ; @r{We will use this dynamically bound}
1075 ; @r{variable to store a closure.}
1076
1077(let ((x 0)) ; @r{@code{x} is lexically bound.}
1078 (setq my-ticker (lambda ()
1079 (setq x (1+ x)))))
1080 @result{} (closure ((x . 0)) ()
1081 (setq x (1+ x)))
1082
1083(funcall my-ticker)
1084 @result{} 1
1085
1086(funcall my-ticker)
1087 @result{} 2
1088
1089(funcall my-ticker)
1090 @result{} 3
1091
1092x ; @r{Note that @code{x} has no global value.}
1093@error{} Symbol's value as variable is void: x
1094@end example
1095
1096@noindent
1097The @code{let} binding defines a lexical environment in which the
1098variable @code{x} is locally bound to 0. Within this binding
1099construct, we define a lambda expression which increments @code{x} by
1100one and returns the incremented value. This lambda expression is
1101automatically turned into a closure, in which the lexical environment
1102lives on even after the @code{let} binding construct has exited. Each
1103time we evaluate the closure, it increments @code{x}, using the
1104binding of @code{x} in that lexical environment.
1105
1106 Note that unlike dynamic variables which are tied to the symbol
1107object itself, the relationship between lexical variables and symbols
1108is only present in the interpreter (or compiler). Therefore,
1109functions which take a symbol argument (like @code{symbol-value},
1110@code{boundp}, and @code{set}) can only retrieve or modify a
1111variable's dynamic binding (i.e., the contents of its symbol's value
1112cell).
1113
1018@node Dynamic Binding 1114@node Dynamic Binding
1019@subsection Dynamic Binding 1115@subsection Dynamic Binding
1020 1116
1021 By default, the local variable bindings made by Emacs are dynamic 1117 Local variable bindings are dynamic in the modern Lisp dialect for
1022bindings. When a variable is dynamically bound, its current binding 1118special variables, and for all variables in the old Lisp
1119dialect. (@xref{Using Lexical Binding}.)
1120Dynamic variable bindings have their uses but are in general more
1121error-prone and less efficient than lexical bindings, and the compiler
1122is less able to find mistakes in code using dynamic bindings.
1123
1124 When a variable is dynamically bound, its current binding
1023at any point in the execution of the Lisp program is simply the most 1125at any point in the execution of the Lisp program is simply the most
1024recently-created dynamic local binding for that symbol, or the global 1126recently-created dynamic local binding for that symbol, or the global
1025binding if there is no such local binding. 1127binding if there is no such local binding.
@@ -1086,9 +1188,6 @@ a dynamic local binding, Emacs records the contents of the value cell
1086value cell. When the binding construct finishes executing, Emacs pops 1188value cell. When the binding construct finishes executing, Emacs pops
1087the old value off the stack, and puts it in the value cell. 1189the old value off the stack, and puts it in the value cell.
1088 1190
1089 Note that when code using Dynamic Binding is native compiled the
1090native compiler will not perform any Lisp specific optimization.
1091
1092@node Dynamic Binding Tips 1191@node Dynamic Binding Tips
1093@subsection Proper Use of Dynamic Binding 1192@subsection Proper Use of Dynamic Binding
1094 1193
@@ -1135,109 +1234,6 @@ variables like @code{case-fold-search}:
1135@end example 1234@end example
1136@end itemize 1235@end itemize
1137 1236
1138@node Lexical Binding
1139@subsection Lexical Binding
1140
1141 Lexical binding was introduced to Emacs, as an optional feature, in
1142version 24.1. We expect its importance to increase with time.
1143Lexical binding opens up many more opportunities for optimization, so
1144programs using it are likely to run faster in future Emacs versions.
1145Lexical binding is also more compatible with concurrency, which was
1146added to Emacs in version 26.1.
1147
1148 A lexically-bound variable has @dfn{lexical scope}, meaning that any
1149reference to the variable must be located textually within the binding
1150construct. Here is an example
1151@iftex
1152(see the next subsection, for how to actually enable lexical binding):
1153@end iftex
1154@ifnottex
1155(@pxref{Using Lexical Binding}, for how to actually enable lexical binding):
1156@end ifnottex
1157
1158@example
1159@group
1160(let ((x 1)) ; @r{@code{x} is lexically bound.}
1161 (+ x 3))
1162 @result{} 4
1163
1164(defun getx ()
1165 x) ; @r{@code{x} is used free in this function.}
1166
1167(let ((x 1)) ; @r{@code{x} is lexically bound.}
1168 (getx))
1169@error{} Symbol's value as variable is void: x
1170@end group
1171@end example
1172
1173@noindent
1174Here, the variable @code{x} has no global value. When it is lexically
1175bound within a @code{let} form, it can be used in the textual confines
1176of that @code{let} form. But it can @emph{not} be used from within a
1177@code{getx} function called from the @code{let} form, since the
1178function definition of @code{getx} occurs outside the @code{let} form
1179itself.
1180
1181@cindex lexical environment
1182 Here is how lexical binding works. Each binding construct defines a
1183@dfn{lexical environment}, specifying the variables that are bound
1184within the construct and their local values. When the Lisp evaluator
1185wants the current value of a variable, it looks first in the lexical
1186environment; if the variable is not specified in there, it looks in
1187the symbol's value cell, where the dynamic value is stored.
1188
1189@cindex closures, example of using
1190 Lexical bindings have indefinite extent. Even after a binding
1191construct has finished executing, its lexical environment can be
1192``kept around'' in Lisp objects called @dfn{closures}. A closure is
1193created when you define a named or anonymous function with lexical
1194binding enabled. @xref{Closures}, for details.
1195
1196 When a closure is called as a function, any lexical variable
1197references within its definition use the retained lexical environment.
1198Here is an example:
1199
1200@example
1201(defvar my-ticker nil) ; @r{We will use this dynamically bound}
1202 ; @r{variable to store a closure.}
1203
1204(let ((x 0)) ; @r{@code{x} is lexically bound.}
1205 (setq my-ticker (lambda ()
1206 (setq x (1+ x)))))
1207 @result{} (closure ((x . 0)) ()
1208 (setq x (1+ x)))
1209
1210(funcall my-ticker)
1211 @result{} 1
1212
1213(funcall my-ticker)
1214 @result{} 2
1215
1216(funcall my-ticker)
1217 @result{} 3
1218
1219x ; @r{Note that @code{x} has no global value.}
1220@error{} Symbol's value as variable is void: x
1221@end example
1222
1223@noindent
1224The @code{let} binding defines a lexical environment in which the
1225variable @code{x} is locally bound to 0. Within this binding
1226construct, we define a lambda expression which increments @code{x} by
1227one and returns the incremented value. This lambda expression is
1228automatically turned into a closure, in which the lexical environment
1229lives on even after the @code{let} binding construct has exited. Each
1230time we evaluate the closure, it increments @code{x}, using the
1231binding of @code{x} in that lexical environment.
1232
1233 Note that unlike dynamic variables which are tied to the symbol
1234object itself, the relationship between lexical variables and symbols
1235is only present in the interpreter (or compiler). Therefore,
1236functions which take a symbol argument (like @code{symbol-value},
1237@code{boundp}, and @code{set}) can only retrieve or modify a
1238variable's dynamic binding (i.e., the contents of its symbol's value
1239cell).
1240
1241@node Using Lexical Binding 1237@node Using Lexical Binding
1242@subsection Using Lexical Binding 1238@subsection Using Lexical Binding
1243 1239