Emacs Lisp uses indefinite scope for local variable bindings. This means that any function anywhere in the program text might access a given binding of a variable. Consider the following function definitions:
(defun binder (x) ;x
is bound inbinder
. (foo 5)) ;foo
is some other function. (defun user () ;x
is used ``free'' inuser
. (list x))
In a lexically scoped language, the binding of x
in
binder
would never be accessible in user
, because
user
is not textually contained within the function
binder
. However, in dynamically scoped Emacs Lisp, user
may or may not refer to the binding of x
established in
binder
, depending on circumstances:
user
directly without calling binder
at all,
then whatever binding of x
is found, it cannot come from
binder
.
foo
as follows and then call binder
, then the
binding made in binder
will be seen in user
:
(defun foo (lose) (user))
foo
as follows and then call binder
,
then the binding made in binder
will not be seen in
user
:
(defun foo (x) (user))Here, when
foo
is called by binder
, it binds x
.
(The binding in foo
is said to shadow the one made in
binder
.) Therefore, user
will access the x
bound
by foo
instead of the one bound by binder
.
Emacs Lisp uses dynamic scoping because simple implementations of lexical scoping are slow. In addition, every Lisp system needs to offer dynamic scoping at least as an option; if lexical scoping is the norm, there must be a way to specify dynamic scoping instead for a particular variable. It might not be a bad thing for Emacs to offer both, but implementing it with dynamic scoping only was much easier.
Go to the first, previous, next, last section, table of contents.