This document describes other important features and mechanisms which are part of the language
or class library, but have been ignored until now.
Beside globals, method locals and instance variables,
there are also:
Block- and Methodvariables are stored in so called Contexts.
These are created for every method- or block invocation.
They have to major purposes:
- Class Instance Variables
Because classes are also objects, they too can have additional private variable slots,
much the same as normal objects can. A class instance variable's definition is inherited by
a subclass; however, the subclass may place its own value into its slot.
That means, once defined by a class, the class and all of its subclasses have that slot,
but each one may put a different value into it.
Class instance variables are seen by class methods only.
Their lifetime is the lifetime of the class.
- Class Variables (also called "Statics" in newer literature)
These are defined in the class definition. Their lifetime is that of the class.
They are visible both from class- and from instance methods.
They are shared with and seen by subclasses.
This means, that in contrast to the above class instance variables,
subclasses do not have their own private slot. Instead, they all share the single variable
cell that the defining class created.
- Pool Variables (in Pool Dictionaries)
Pool variables are seen by all classes which "subscribe" to a given pool.
Then all the variables defined by that pool become visible and accessible within a class on
both the class and instance side. The pool itself is actually a implemented as class
and the pool variables are its class variables. Thus the pool's initialization code
(#initialize method on the class side) typically sets initial values for the pool variables.
- Block Local Variables
Block locals are similar to method locals.
They are seen within the block.
This includes all containing sub-blocks.
Due to the closure-behavior of both block- and methodContexts,
these variables may outlife their defining block's life.
This happens, whenever a subblock which referes to such a variable
is exposed to somewhere outside the block. For example, when a subblock is stored into
a variable or another object, or if it is returned as a value.
In other programming languages, these would be called Stack Frame, and that's what they
are most of the time. However, via the pseudo variable thisContext,
they can be accessed at any time and treated like any other first class object:
you can store them in other objects or return them as value.
- to remember the caller's context and keep the current pc (for proper return to the caller)
- to store local variables (method- and block variables)
- to provide space for intermediate values (expression evaluation stack)
C-programmers should notice that the Smalltalk runtime system
makes sure that the memory space used by a context gets moved from the stack
to the heap automatically, whenever required. As a programmer, this movement
is completely transparent.
Continue in "More Smalltalk".
Copyright © 1996 Claus Gittinger Development & Consulting
Copyright © eXept Software AG
<cg at exept.de>
Doc $Revision: 1.8 $ $Date: 2021/03/13 18:24:49 $