Static Scope refers to the scope of variables in a programming language. When a language is statically scoped, variable references will always refer to the same variable even in different contexts. For example, in Scheme:

(define x 5)
(define +x
    (lambda (y)
       (+ x y)))

(+x 5) ; evaluates to 10
(let ((x 10))
   (+x 5)) ; Also evaluates to ten
           ; it would evaluate to 15 in a language
           ; that used Dynamic Scope

When a function is defined, it carries the current lexical environment (LexicalEnvironment) along with it so all variable references will occur in the environment that the function was defined in. This is why StaticScope is also called LexicalScope. Compare StaticScope to DynamicScope.

CommonLispLanguage, MlLanguage, and SchemeLanguage are examples of Statically Scoped languages.

Actually, CommonLispLanguage offers DynamicScope (with SpecialVariables, which can be defined with defvar or defparameter) as well as LexicalScope. The equivilant to the example above would, in CommonLispLanguage, work exactly the same as the EmacsLispLanguage example on the DynamicScope page, i.e., the evaluation of the let form would return 15, not 10.

StaticScope (last edited 2008-07-09 05:47:56 by localhost)