Programming language variables: scope and extent
Static versus dynamic
Before we can start our examination, we need to establish how the workings of variables can be observed. The following are two ways of looking at program code.
Variable bindings and their scope and extent
var x = ...;
establishes a binding
for the variable x
that maps the variable name to a value. In the following, the terms “variable” and “binding” are often used interchangeably. This is less precise, but makes explanations shorter. Bindings are characterized by:
- Scope (a spatial property): Where can the binding be accessed?
- Extent (a temporal property): How long does the binding exist?
There are several variations of implementing scope and extent in a programming language.
Most modern programming languages use static scoping. Some, such as Common Lisp, additionally support dynamic scoping (for dynamically encapsulated “global” variables). Two effects influence scoping:
the most common kinds of extent are
Note that there exist many different definitions of “extent” in the literature. We have covered just one of them .
- Scope and Extent (chapter 3 in “Common Lisp the Language” by Guy Steele). This chapter is the source of part of this post.