[Newbies] Object new
cdshaffer at acm.org
Thu Jun 27 13:55:16 UTC 2019
I think David’s exploratory approach is the best way to understand the answer to your question. Here’s an explanation that will either thoroughly confuse you or make you love Smalltalk for its consistency.
Smalltalk’s adhere to a few rule:
1) Every “thing” in Smalltalk is an object
2) Every object has a class
3) Every class has a superclass (which may be nil in a few special cases)
So, as a silly example, 5 is an object. It is an instance of the class SmallInteger which is a subclass of the class Integer. Objects get their methods from their classes so if I want to know what kinds of things 5 can do, I’d look at methods in SmallInteger, Integer and up the hierarchy.
So…since everything is an object, classes must be objects. The question is, what class are classes instances of? More specifically, what class is the class SmallInteger an instance of. The answer is a class call “SmallInteger class” which is a subclass of "Integer class" etc. Where does SmallInteger get its methods? The answer is, of course "SmallInteger class" and up the hierarchy. So there’s a parallel hierarchy of these “things that are the classes of classes.” BTW, these are called metaclasses. Also, the methods in metaclasses show on on the “Class side” of the browser. So, if you’re looking at the class-side methods of the class Integer, you’re actually looking at the instance-side methods of the metaclass “Integer class”.
So, to your question. When you send Object new you are sending a message to the Object class. Where are its methods? In the metaclass “Object class”, of course. When you look at the class side of Object, you don’t see #new. You look at the class side of Object’s superclass, ProtoObject, still no #new. ProtoObject is a subclass of nil so you think, crap, where did #new come from? The truth is, the class browser is failing you here. Yes, ProtoObject’s super class is nil but that’s not the thing you care about. What you care about is the superclass of “ProtoObject class”, right? So you run to a workspace and type:
ProtoObject class superclass ====> Class
Ah, the superclass of “ProtoObject class” isn’t nil! Its a class called Class. So, now you can look at the (instance side) methods in Class and continue to work your way up the hierarchy. You’ll find #new, just as you thought, in Behavior.
You might be wondering where this all ends. Let’s go through it:
5 class ===> SmallInteger
SmallInteger class ===> SmallInteger class Its like Smalltalk is mocking me :)
SmallInteger class class ===> Metaclass So, these “metaclasses” are instances of a class called Metaclass, make sense!
Metaclass class ===> Metaclass class So, Metaclass’s class is a Metaclass called “Metaclass class”, ouch
Metaclass class class ===> Metaclass Here’s the “end” (a loop)
Smalltalk’s simple rules lead to a Metaclass system. It is highly consistent (you can /always/ ask an object for its class and you will get back something that is not nil). Many Object-Oriented programming languages do not have the parallel hierarchy metaclass system. In Java, for example, if you ask an object for its class you get back an /instance/ of the class Class. This class (Class), is the only metaclass in Java.
A picture is worth a thousand words (I apologize for lack of proper attribution of this PDF file, I’ve lost track of where I got it over the years). Note: Smalltalks vary and some, like the one in this figure, do not have ProtoObject as a superclass of Object.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Class - Metaclass Structure.pdf
Size: 13321 bytes
Desc: not available
-------------- next part --------------
Hope that helps!
> On Jun 27, 2019, at 8:01 AM, John McCulloch <johnnymcc9 at gmail.com> wrote:
> I was trying to decide whether I wanted to use GNU Smalltalk or Squeak. My
> version of Ubuntu doesn't have a version of of gst with GUI that works so
> I'm going with Squeak. This issue applies to both anyway. It's not a
> problem but more of an issue about understanding how stuff works.
> The issue is with creating new classes by including "Object new". The
> Object class doesn't have a #new method. ProtoObject doesn't either.
> Class Behavior has one but that is a subClass of Object. Is this the #new
> that gets called and if so, how does Squeak know to use it? If it's
> somewhere else, where is it?
> Sent from: http://forum.world.st/Squeak-Beginners-f107673.html
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
More information about the Beginners