[newbie] how does Object implement new?

David N. Smith (IBM) dnsmith at watson.ibm.com
Thu Mar 29 19:58:19 UTC 2001


At 11:48 -0500 3/29/01, Karl Wilson wrote:
>Merrily working away on the _Squeak OO Design with Multimedia Applications_ book. Joe the Box. I get curious about how "new" is implemented.
>
>How does Object (or ProtoObject) implement new?
>
>When you write Box>>new (Box is a subclass of Object):
>
>new
>	^super new initialize.
>
>where is the new that it is finding?
>
>I found Behavior>>new, which calls a primitive, which seems a likely possibility, but I can't see how Object uses it?

A ha! You've entered the realm of MetaClasses, which are beautiful but very confusing at first. The simplest thing is to ignore them.  (Pause while you escape)   If you are still reading then here goes.


One way to see what is going on is to ask; expressions below are shown left justified, and the results are indented:

String new class
 String

The class of an instance of String is String; not at all an unexpected result!


String class
 String class

The class of String is something called 'String class'. It seems not to have any other name (and doesn't).


String class class
 Metaclass

However, the class of the class is Metaclass, so somewhere there must be a class named Metaclass, and one of its instances is the class of String. (It's OK to mumble to yourself at this point).


String class class class
 Metaclass class

Taking it one more step and we get another unnamed thing.


String class class class class
 Metaclass

And yet one more step and we're back at Metaclass! Could it mean that Metaclass is an instance of an instance of itself?



Let's look at the world from a different direction. What are the parents of ProtoObject and its class?

ProtoObject superclass
 nil

It looks like ProtoObject has no super class. (Many people say that it's a subclass of nil, but strictly speaking that is false.)


ProtoObject class superclass
 Class

A ha! The super class of ProtoObject class is a class named Class. (Things get a bit hard to say aloud.)

Now note that Behavior is the parent of both Class and Metaclass.

Also note the critical fact that the hierarchy for instances stops at ProtoObject, but for classes jumps to class Class. IE, there is a 'goto' in the hierarchy for classes, and note that it jumps to the instance hierarchy. The #new method of Behavior is an INSTANCE method. (It's OK if you mumble louder; try not to scream as it alarms the neighbors and doesn't help at all).


I've always been enchanted by all this --- how classes are instances of instances of a class, how the class hierarchy for classes has that neat 'goto', how Metaclass is an instance of an instance of itself. It's all so elegant and beautiful. However, one should wonder about either the genius or sanity of whomever thought this all up.  :-)

Have Fun!

Dave
-- 
_______________________________
David N. Smith
IBM T J Watson Research Center
Hawthorne, NY
_______________________________
Any opinions or recommendations
herein are those of the author  
and not of his employer.





More information about the Squeak-dev mailing list