[ < ] | [ > ] | [ << ] | [Plus haut] | [ >> ] | [Top] | [Table des matières] | [Index] | [ ? ] |
Global variables have values that last until explicitly superseded with new values. Sometimes it is useful to create variable values that exist temporarily—only until a certain part of the program finishes. These values are called local, and the variables so used are called local variables.
For example, when a function is called, its argument variables receive new
local values that last until the function exits. The let
special
form explicitly establishes new local values for specified variables; these
last until exit from the let
form.
Establishing a local value saves away the previous value (or lack of one) of the variable. When the life span of the local value is over, the previous value is restored. In the mean time, we say that the previous value is shadowed and not visible. Both global and local values may be shadowed (voir la section Scope).
If you set a variable (such as with setq
) while it is local, this
replaces the local value; it does not alter the global value, or previous
local values, that are shadowed. To model this behavior, we speak of a
local binding of the variable as well as a local value.
The local binding is a conceptual place that holds a local value. Entry to
a function, or a special form such as let
, creates the local binding;
exit from the function or from the let
removes the local binding. As
long as the local binding lasts, the variable's value is stored within it.
Use of setq
or set
while there is a local binding stores a
different value into the local binding; it does not create a new binding.
We also speak of the global binding, which is where (conceptually) the global value is kept.
A variable can have more than one local binding at a time (for example, if
there are nested let
forms that bind it). In such a case, the most
recently created local binding that still exists is the current
binding of the variable. (This rule is called dynamic scoping; see
Scoping Rules for Variable Bindings.) If there are no local bindings, the variable's
global binding is its current binding. We sometimes call the current
binding the most-local existing binding, for emphasis. Ordinary
evaluation of a symbol always returns the value of its current binding.
The special forms let
and let*
exist to create local bindings.
This special form binds variables according to bindings and then
evaluates all of the forms in textual order. The let
-form
returns the value of the last form in forms.
Each of the bindings is either (i) a symbol, in which case that
symbol is bound to nil
; or (ii) a list of the form
(symbol value-form)
, in which case symbol is bound
to the result of evaluating value-form. If value-form is
omitted, nil
is used.
All of the value-forms in bindings are evaluated in the order
they appear and before binding any of the symbols to them. Here is
an example of this: z
is bound to the old value of y
, which is
2, not the new value of y
, which is 1.
(setq y 2) ⇒ 2 (let ((y 1) (z y)) (list y z)) ⇒ (1 2) |
This special form is like let
, but it binds each variable right after
computing its local value, before computing the local value for the next
variable. Therefore, an expression in bindings can reasonably refer
to the preceding symbols bound in this let*
form. Compare the
following example with the example above for let
.
(setq y 2)
⇒ 2
(let* ((y 1)
(z y)) ; Use the just-established value of |
Here is a complete list of the other facilities that create local bindings:
condition-case
(voir la section Errors).
Variables can also have buffer-local bindings (voir la section Buffer-Local Variables) and frame-local bindings (voir la section Frame-Local Variables); a few variables have terminal-local bindings (voir la section Multiple Displays). These kinds of bindings work somewhat like ordinary local bindings, but they are localized depending on “where” you are in Emacs, rather than localized in time.
This variable defines the limit on the total number of local variable
bindings and unwind-protect
cleanups (voir la section Cleaning Up from Nonlocal Exits) that are allowed before signaling an error (with data
"Variable binding depth exceeds max-specpdl-size"
).
This limit, with the associated error when it is exceeded, is one way that
Lisp avoids infinite recursion on an ill-defined function.
max-lisp-eval-depth
provides another limit on depth of nesting.
Voir Eval.
The default value is 1000. Entry to the Lisp debugger increases the value, if there is little room left, to make sure the debugger itself has room to execute.
[ < ] | [ > ] | [ << ] | [Plus haut] | [ >> ] | [Top] | [Table des matières] | [Index] | [ ? ] |
Ce document a été généré par Eric Reinbold le 13 Octobre 2007 en utilisant texi2html 1.78.