next up previous
Next: Lisp Objects and Data Up: Variables Previous: Global and 'Local' Variables

Scoping Rules

The Elisp manual says Local bindings in Emacs Lisp have ``indefinite scope'' and ``dynamic extent''. "Scope" refers to where textually in the source code the binding can be accessed. "Indefinite scope" means that any part of the program can potentially access the variable binding. "Extent" refers to when, as the program is executing, the binding exists. "Dynamic extent" means that the binding lasts as long as the activation of the construct that established it.

You can use that to impress your friends; if you can say it with a straight face. But, what does it really mean? The heart of the matter is that if you look at a lisp function definition, say, and find some variables used, for which no binding is apparent in the function itself, there is no cause for worry. It is alright as long as at the time of execution of that function the variable has a binding. This is very different from a language like C, where a variable should have a binding in the same textual block as the usage.

(defun foo ()
   (message "bar is \%s" bar))
(let ((bar "foo"))
  (foo))

In the above example, function foo is using bar which appears to be a free variable. When foo is invoked as shown inside a let statement, foo gets bound to a value, although the place of usage is inside another function. If, however, foo is invoked without providing a local binding as shown, then there will be a runtime error pointing out that foo has no binding.

Common Lisp is a complicated beast. Its variables can have either lexical scope (i.e. C like scoping behaviour), or dynamic scoping (explained above). Elisp has only dynamic scoping.

If you really want to know more about scoping and really want to get more confused, you should feel free to refer to the Elisp manual.


next up previous
Next: Lisp Objects and Data Up: Variables Previous: Global and 'Local' Variables
Sriram Karra
2005-01-06