[Newbies] Object new
johnnymcc9 at gmail.com
Fri Jun 28 12:16:08 UTC 2019
So when looking at how Smalltalk finds the implementation of a method,
one should consider sending a message to a class is really sending a
message to an instance of it's metaclass? If you look at it this way
and follow the path starting at "Object class" it leads you to
Behavior. It seems like this works differently than for user created
classes where a class's superclass is the same as it's metaclass'
Btw, I think the diagram applies almost exactly to GNU Smalltalk's model.
On 6/27/19 9:55 AM, David Shaffer wrote:
> 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.
> 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
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners