Re: What different between Instance var, method & Class var,method?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: What different between Instance var, method & Class var,method?

David Simmons
"¦¶ÂfÂc" <[hidden email]> wrote in message
news:90tp3d$[hidden email]...
> Hi~ I'm a beginner in smalltalk, I want to ask what different between
> "Instance variable" "Instance method" and "Class variable" "Class
method"??
>
> Thank you very much~
>
> Leo
>

All objects conceptually have a memory structure like:

[hdr/properties]
    [named slots]
        [indexed slots]
            [byte-vars/structured storage fields]

typedef struct AnObject /* c++ style logical view */
{
    ObjHeader   header;
    OOP         firstNamedVar;
    ...
    OOP         islots[nIndexedSlots]
    ...
    union {
        char    byteVars[nByteVars];
        short   wordVars[nWordVars];
        struct FieldLayout {
        } field;
    };
};

classes are also real objects and are subclasses of the <Behavior>
heirarchy. They describe, among other things, the layout of their instances
and the behaviors (methods) that their instances can respond to (perform).
So INSTANCE METHODS are the set of behavior that an instance can
respond-to/perform. Instance methods are maintained (logically) in their
class its superclasses and any mixed in <Interfaces>.

metaclasses are also real object and are subclasses of the <Behavior>
heirarchy. They describe, among other things, the layout of their instances
(which are classes) and the behaviors (methods) that their instances can
respond to (perform). So CLASS METHODS are the set of behavior that a class
can respond-to/perform. Class methods are maintained (logically) in the
classes' metaclass its superclasses and any mixed in <Interfaces>.

Restating the previous paragraph for clarity:
--------------------------------------------
Because classes are also objects, they too must have a "behavior". That
behavior is a metaclass. A metaclass class correponds to a class in exactly
the same way that a class describes the layouts and methods available for
its instances. I.e., because a class is the singleton instance of a
metaclass. Metaclasses describe the layout and behavior for their instances
(which are classes).

Note: metaclasses describe the operations (methods) a class can perform.
metaclasses are instances of <Metaclass>. Because of this reason,
metaclasses are where most of the metadata and metaoperations for the
"class-metaclass" pair reside. By placing them in the metaclass, the
potential conflict between user defined class behavior (class methods) and
metadata operations (constructor/behavior methods) is minimized. In other
words, metaclasses generally contain the metadata for a "class-metaclass"
pair. Also, class objects are singleton instances of their metaclass. I.e.,
there is a one-to-one relationship between a class and a metaclass.

Part of the metadata of a class-metaclass pair is the set of "shared
class-pool variables" that all instance methods of a class and its
subclasses can access, as well as the all the methods of the class itself
and its subclasses.

To obtain a "class-metaclass", they are maintained as "variables"
(global/scoped/pool) -- described below. The <value> of a variable holding a
class is the class itself. The class of a class is its metaclass. The <key>
of a variable holding a class is a symbol representing its name. If the
SmallScript/Smalltalk dialect supports scoped selectors (<Symbols>) then the
<key> will be a symbol whose scope is the namespace to which the variable
(and thus class) belongs.
---
So with that background we have:

anyObject --described by--> anyObject's class

    anyObject's class --described by--> anyObject's metaclass

        anyObject's metaclass --described by--> <Metaclass>


Now, we can answer the rest of your question:

An INSTANCE VARIABLE is a "named slot" in an instance of an object. The name
of the slot and its corresponding physical index within the objects data
structure is (logically) described by its class.

    "Any object"
    [hdr][named slots][indexed slots]...

Exactly like an "instance variable", a CLASS (instance) VARIABLE is a "named
slot" in an instance of a class (object). The name of the slot and its
corresponding physical index within the objects data structure is
(logically) described by that classes' metaclass.

    "A class object is like any other object"
    [hdr][named slots][indexed slots]...

A SHARED (class) VARIABLE is a variable defined in the metadata, usually
implemented via the "shared class-pool variables". Where, the metadata
contains a (pool) dictionary of <Association> instances or something similar
<key> and <value> class like <PoolVariable> in SmallScript/QKS Smalltalk.
"Shared variables" logically have a <key> that is the variables "name" and a
<value> that is the contents of the variable. When a method is compiled that
references a "shared variable", the variable object itself (an
<Association/PoolVariable>) is stored in the method's literals.

[hdr/properties]
    [named slots] <key> <value> ...

typedef struct PoolVariable /* c++ style logical view */
{
    ObjHeader   header;
    OOP         key;        // variable's name (a <Symbol>)
    OOP         value;      // variable's value (enables sharing)
    ...
};

The compiler then "logically" generates code to read from the variable via
#value or write to the variable via #value:. The result is that methods can
then "share" that "variable" object and so have common access (closure) over
that variable. See more below...

Depending on the dialect, there may be additional subclasses of
<Association> or <PoolVariable>. In SmallScript/QKS-Smalltalk there are also
<PoolVariableAlias> and <ThreadLocalPoolVariable>.

----
As I promised earlier in the post, here is the description of "variables"
(global/scoped/pool).

Depending on the Smalltalk dialect, there is another form of variable
provided for namespaces.

In some Smalltalk's (such as SmallScript/QKS-Smalltalk), classes are
namespaces. So the old SINGLE namespace of smalltalk-80, maintained in
<Smalltalk> a <SystemDictionary> becomes a user defineable set of
NAMESPACES. I.e., every class is a NAMESAPCE.

This class-is-a-namespace design unifies the concept of a namespace-variable
[aka "scoped-variables", old deprecated term
global-variables/system-dictionary-variables] with that of the smalltalk-80
shared-class-variables (i.e., in the metadata's class-pool we talked about
already).

In other dialects, global (aka public) variables are maintained in a new
form of namespace dictionary, or are maintained (using name mangling) in the
older style (often deprecated in modern Smalltalk implementations) Smalltalk
system dictionary.


-- Dave Simmons [www.qks.com / www.smallscript.com]
  "Effectively solving a problem begins with how you express it."