Check out the new USENIX Web site. next up previous
Next: Types Up: Names, Lifetimes, Types and Previous: Names

Lifetimes

There are three options for the lifetime of a Nickle name, as opposed to C's auto and static. This extra control is a natural consequence of the distinction between global functions and nested functions, which is lacking in C. It manifests noticeably in the times at which initialization occurs. The three lifetimes for a Nickle declaration are:

global  The lifetime of the named value is the lifetime of the interpreter evaluating the program. Objects declared global are initialized once, when the definition of the function containing them is first encountered.
static  The lifetime of the named value is the lifetime of the function in which the definition occurs. Objects declared static are initialized whenever the function containing their definition is evaluated. In the absence of nested scopes, this lifetime is the same as global.
auto  The lifetime of the named value is the lifetime of the current function. Objects declared auto are initialized whenever their definition is evaluated.
The default lifetimes are as in C: global for top-level objects, and auto for those local to a function. Note that lifetime is different than scope: different function definitions, for example, may have different global objects named x.

Figure 2 is useful illustrating the difference between auto, global and static scope. Imagine that the auto declaration of Figure 2 was instead a static declaration. In this case, x would be initialized whenever the definition of g was evaluated, and thus the second invocation of g() at top-level would return 2. If x was instead declared global, it would be initialized only once, when the definition of f was compiled, and thus the successive invocations of g() would return 1, 2, and 3.

Figure 2: A variable x with auto scope.
\begin{figure}\begin{verbatim}int() function f () {
int function g () {
glo...
...
> g = f();
> g()
1
> g()
1
> g = f();
> g()
1\end{verbatim}\end{figure}


next up previous
Next: Types Up: Names, Lifetimes, Types and Previous: Names
Bart Massey 2001-04-19