Metaclasses?
Brian Murphy-Dye
brian.murphydye at mac.com
Wed Oct 13 23:18:40 UTC 2004
Our mental picture of a class is that there is one instance for each
class. For example, we tend to think there is one instance out there
representing the String class, and the browser helps feed this image.
In actuality, there two instances exist for every class: a Metaclass
instance and a Class instance. The Metaclass instance contains the
class methods and class instance variable names for a class. When you
click on the 'class' button in a browser, you are actually looking at
the methods from the Metaclass instance. The Class instance contains
the methods and variables that belong to the instances of the class. So
there are three instances for the string 'abc': the String Metaclass,
the String Class, and the instance containing the characters 'abc'.
Class and Metaclass are just like other classes, so each of them also
have a Class instance and a Metaclass instance.
There is a little magic that goes on at this point because there is a
chicken and egg problem: the 'Class' class can't exist without the
Class instance and Class metaclass, yet how can you have a Class
instance until the Class class exists? I haven't looked into how Squeak
solves this, but 'The Art of Metaobject Protocol' goes into great
detail on one way to solve the bootstrapping problem.
In a way there are three levels of definition for every instance in the
environment:
- metaclass to hold class method/variable information
- class to hold instance method/variable information
- instance to hold instance data
When you are holding an instance and ask it to evaluate a selector, it
determines the code to execute by looking one level higher. For
example, '3 + 5' sends #+ to the instance '3'. The #+ selector is
looked for one level higher at the class. For '3 class name', #class is
sent to '3' which returns the class level (which is the Class instance
for SmallInteger). This method happens to return a Class instance. So
#name is sent to the next level up: the metaclass level.
You noted that:
'Symbol class inspect' is a Metaclass
but
Symbol class = Metaclass ----> false
By the same token, note that
'3 class inspect' is a SmallInteger class
but
3 class = SmallInteger class ----> false
So what shows on the top of the inspector is really one level higher.
Try:
Symbol class class = Metaclass ----> true
Hope this helps a little ...
Brian.
On Oct 13, 2004, at 4:32 PM, Michael van der Gulik wrote:
> Hi all.
>
> I'm getting confused. This is an actual problem I've come across in
> DPON (my vapourware project).
>
> Now, in DPON, objects can be replicated. E.g. a Symbol (like #me) can
> be replicated. To do so, you need to implement
>
> Symbol class>>replicationAlgorithm
>
> to return the replication algorithm. Classes themselves also need to
> be replicated. In that case, I implement
>
> Symbol class>>replicationAlgorithm
>
> but... then Metaclass>>replicationAlgorithm gets called. Fair
> enough... a Metaclass is the class of a Class. But, in Squeak 3.7:
>
> Symbol class = Metaclass (print it...)---> false.
>
> and...
>
> [Symbol class isKindOf: Class] ---> false.
> [Metaclass isKindOf: Class] ---> true.
>
> Hmm.. but if I inspect [Symbol class], I'm looking at Metaclass.
>
> Could somebody explain this to me please?
>
> Michael.
>
>
>
>
>
>
More information about the Squeak-dev
mailing list
|