However, once a value, such as the standard output stream, is stored in a global variable and you have written code that references that global variable, it's tempting to try to temporarily modify the behavior of that code by changing the variable's value.
For instance, suppose you're working on a program that contains some low-level logging functions that print to the stream in the global variable *standard-output*
. Now suppose that in part of the program you want to capture all the output generated by those functions into a file. You might open a file and assign the resulting stream to *standard-output*
. Now the low-level functions will send their output to the file.
This works fine until you forget to set *standard-output*
back to the original stream when you're done. If you forget to reset *standard-output*
, all the other code in the program that uses *standard-output*
will also send its output to the file.[75]
What you really want, it seems, is a way to wrap a piece of code in something that says, "All code below here—all the functions it calls, all the functions they call, and so on, down to the lowest-level functions—should use this value for the global variable *standard-output*
." Then when the high-level function returns, the old value of *standard-output*
should be automatically restored.
It turns out that that's exactly what Common Lisp's other kind of variable—dynamic variables—let you do. When you bind a dynamic variable—for example, with a LET
variable or a function parameter—the binding that's created on entry to the binding form replaces the global binding for the duration of the binding form. Unlike a lexical binding, which can be referenced by code only within the lexical scope of the binding form, a dynamic binding can be referenced by any code that's invoked during the execution of the binding form.[76] And it turns out that all global variables are, in fact, dynamic variables.
Thus, if you want to temporarily redefine *standard-output*
, the way to do it is simply to rebind it, say, with a LET
.
(let ((*standard-output* *some-other-stream*))
(stuff))
In any code that runs as a result of the call to stuff
, references to *standard-output*
will use the binding established by the LET
. And when stuff
returns and control leaves the LET
, the new binding of *standard-output*
will go away and subsequent references to *standard-output*
will see the binding that was current before the LET
. At any given time, the most recently established binding shadows all other bindings. Conceptually, each new binding for a given dynamic variable is pushed onto a stack of bindings for that variable, and references to the variable always use the most recent binding. As binding forms return, the bindings they created are popped off the stack, exposing previous bindings.[77]
A simple example shows how this works.
(defvar *x* 10)
(defun foo () (format t "X: ~d~%" *x*))
The DEFVAR
creates a global binding for the variable *x*
with the value 10. The reference to *x*
in foo
will look up the current binding dynamically. If you call foo
from the top level, the global binding created by the DEFVAR
is the only binding available, so it prints 10.
CL-USER> (foo)
X: 10
NIL
But you can use LET
to create a new binding that temporarily shadows the global binding, and foo
will print a different value.
CL-USER> (let ((*x* 20)) (foo))
X: 20
NIL
Now call foo
again, with no LET
, and it again sees the global binding.
CL-USER> (foo)
X: 10
NIL
Now define another function.
(defun bar ()
(foo)
(let ((*x* 20)) (foo))
(foo))
Note that the middle call to foo
is wrapped in a LET
that binds *x*
to the new value 20. When you run bar
, you get this result:
CL-USER> (bar)
X: 10
X: 20
X: 10
NIL
As you can see, the first call to foo
sees the global binding, with its value of 10. The middle call, however, sees the new binding, with the value 20. But after the LET
, foo
once again sees the global binding.
As with lexical bindings, assigning a new value affects only the current binding. To see this, you can redefine foo
to include an assignment to *x*
.
(defun foo ()
(format t "Before assignment~18tX: ~d~%" *x*)
(setf *x* (+ 1 *x*))
(format t "After assignment~18tX: ~d~%" *x*))
Now foo
prints the value of *x*
, increments it, and prints it again. If you just run foo
, you'll see this:
CL-USER> (foo)
Before assignment X: 10
After assignment X: 11
NIL
Not too surprising. Now run bar
.
CL-USER> (bar)
Before assignment X: 11
After assignment X: 12
Before assignment X: 20
After assignment X: 21
Before assignment X: 12
After assignment X: 13
NIL
Notice that *x*
started at 11—the earlier call to foo
really did change the global value. The first call to foo
from bar
increments the global binding to 12. The middle call doesn't see the global binding because of the LET
. Then the last call can see the global binding again and increments it from 12 to 13.
So how does this work? How does LET
know that when it binds *x*
it's supposed to create a dynamic binding rather than a normal lexical binding? It knows because the name has been declared special.[78] The name of every variable defined with DEFVAR
and DEFPARAMETER
is automatically declared globally special. This means whenever you use such a name in a binding form—in a LET
or as a function parameter or any other construct that creates a new variable binding—the binding that's created will be a dynamic binding. This is why the *naming* *convention*
is so important—it'd be bad news if you used a name for what you thought was a lexical variable and that variable happened to be globally special. On the one hand, code you call could change the value of the binding out from under you; on the other, you might be shadowing a binding established by code higher up on the stack. If you always name global variables according to the *
naming convention, you'll never accidentally use a dynamic binding where you intend to establish a lexical binding.
It's also possible to declare a name locally special. If, in a binding form, you declare a name special, then the binding created for that variable will be dynamic rather than lexical. Other code can locally declare a name special in order to refer to the dynamic binding. However, locally special variables are relatively rare, so you needn't worry about them.[79]
75
The strategy of temporarily reassigning *standard-output* also breaks if the system is multithreaded—if there are multiple threads of control trying to print to different streams at the same time, they'll all try to set the global variable to the stream they want to use, stomping all over each other. You could use a lock to control access to the global variable, but then you're not really getting the benefit of multiple concurrent threads, since whatever thread is printing has to lock out all the other threads until it's done even if they want to print to a different stream.
76
The technical term for the interval during which references may be made to a binding is its
77
Though the standard doesn't specify how to incorporate multithreading into Common Lisp, implementations that provide multithreading follow the practice established on the Lisp machines and create dynamic bindings on a per-thread basis. A reference to a global variable will find the binding most recently established in the current thread, or the global binding.