Version 4 of scope

Updated 2004-08-22 06:12:35 by suchenwi

Scope is a context within which to resolve names to values.

Tcl has many scopes:

namespace is an hierarchal scope for resolving names of commands and variables. The variable command references a variable name in namespace scope.

global scope is a distinguished namespace scope, equivalent to namespace :: in some interpreter

hidden scope is a per-interpreter scope for interp hidden commands

call frame scope is the context of some call frame in the current evaluation.

proc local scope is the context within which all otherwise unqualified variable names in a process are defined. Proc local is a special case of a call frame scope, being the nearest enclosing call frame scope.


In summary, there are two major kinds of scoping in tcl - dynamic and lexical (or static) scope.

dynamic scope is accessed by default inside a proc, or by means of upvar with unqualified names, and is preferred for variable name resolution.

static or lexical scope is accessed explicitly by namespace encoded names, the global operator, and implicitly by command invocation and by the variable operator. Command resolution prefers dynamic scope.

RS has learnt this distinction, however:

  • dynamic scope determines variable value at runtime - used in older Lisp versions
  • lexical scope conserves the value of variables they had at the time a proc was defined, for instance in closures - considered more advanced in Lisp circles

Category Concept