Common Lisp the Language, 2nd Edition

next up previous contents index
Next: Special Forms Up: Forms Previous: Self-Evaluating Forms

5.1.2. Variables

Symbols are used as names of variables in Common Lisp programs. When a symbol is evaluated as a form, the value of the variable it names is produced. For example, after doing (setq items 3), which assigns the value 3 to the variable named items, then items => 3. Variables can be assigned to, as by setq, or bound, as by let. Any program construct that binds a variable effectively saves the old value of the variable and causes it to have a new value, and on exit from the construct the old value is reinstated.

There are actually two kinds of variables in Common Lisp, called lexical (or static) variables and special (or dynamic) variables. At any given time either or both kinds of variable with the same name may have a current value. Which of the two kinds of variable is referred to when a symbol is evaluated depends on the context of the evaluation. The general rule is that if the symbol occurs textually within a program construct that creates a binding for a variable of the same name, then the reference is to the variable specified by the binding; if no such program construct textually contains the reference, then it is taken to refer to the special variable of that name.

The distinction between the two kinds of variable is one of scope and extent. A lexically bound variable can be referred to only by forms occurring at any place textually within the program construct that binds the variable. A dynamically bound (special) variable can be referred to at any time from the time the binding is made until the time evaluation of the construct that binds the variable terminates. Therefore lexical binding of variables imposes a spatial limitation on occurrences of references (but no temporal limitation, for the binding continues to exist as long as the possibility of reference remains). Conversely, dynamic binding of variables imposes a temporal limitation on occurrences of references (but no spatial limitation). For more information on scope and extent, see chapter 3.

The value a special variable has when there are currently no bindings of that variable is called the global value of the (special) variable. A global value can be given to a variable only by assignment, because a value given by binding is by definition not global.

It is possible for a special variable to have no value at all, in which case it is said to be unbound. By default, every global variable is unbound unless and until explicitly assigned a value, except for those global variables defined in this book or by the implementation already to have values when the Lisp system is first started. It is also possible to establish a binding of a special variable and then cause that binding to be valueless by using the function makunbound. In this situation the variable is also said to be ``unbound,'' although this is a misnomer; precisely speaking, it is bound but valueless. It is an error to refer to a variable that is unbound.

X3J13 voted in June 1989 (UNDEFINED-VARIABLES-AND-FUNCTIONS)   to specify more precisely the effects of referring to an unbound variable.

Reading an unbound variable or an undefined function must be detected in the highest safety setting (see the safety quality of the optimize declaration specifier) but the effect is undefined in any other safety setting. That is, reading an unbound variable should signal an error and reading an undefined function should signal an error. (``Reading a function'' includes both references to the function using the function special form, such as f in (function f), and references to the function in a call, such as f in (f x y).)

For the case of inline functions (in implementations where they are supported), a permitted point of view is that performing the inlining constitutes the read of the function, so that an fboundp check need not be done at execution time. Put another way, the effect of the application of fmakunbound to a function name on potentially inlined references to that function is undefined.

When an unbound variable is detected an error of type unbound-variable is signaled, and the name slot of the unbound-variable condition is initialized to the name of the offending variable.

When an undefined function is detected an error of type undefined-function is signaled, and the name slot of the undefined-function condition is initialized to the name of the offending function.

The condition type unbound-slot, which inherits from cell-error, has an additional slot instance, which can be initialized using the :instance keyword to make-condition. The function unbound-slot-instance accesses this slot.

The type of error signaled by the default primary method for the CLOS slot-unbound generic function is unbound-slot. The instance slot of the unbound-slot condition is initialized to the offending instance and the name slot is initialized to the name of the offending variable.

Certain global variables are reserved as ``named constants.'' They have a global value and may not be bound or assigned to. For example, the symbols t and nil are reserved. One may not assign a value to t or nil, and one may not bind t or nil. The global value of t is always t, and the global value of nil is always nil. Constant symbols defined by defconstant also become reserved and may not be further assigned to or bound (although they may be redefined, if necessary, by using defconstant again). Keyword symbols, which are notated with a leading colon, are reserved and may never be assigned to or bound; a keyword always evaluates to itself.

next up previous contents index
Next: Special Forms Up: Forms Previous: Self-Evaluating Forms