Object new

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

Object new

John McCulloch
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Object new

David T. Lewis
Hi John,

On Thu, Jun 27, 2019 at 07:01:31AM -0500, John McCulloch 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.

Welcome :-)

>
> 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?
>

Yes, you have it right. It is calling Behavior>>new.

You can explore this interactively as follows:

- Open a new workspace, and type "Object new" in the workspace.

- Highlight "Object new" and do a right-click to bring up a menu of options.

- Select "Debug it" from the menu.

- This will begin evaluation of the "Object new" expression, running in a debugger.

- Click the "Into" button in the debugger to step into the "new" method, which
is an instance-side method in class Behavior.

You are actually exploring a tricky and interesting part of the system
at this point. To get an idea of how it works, try this:

- In your workspace, type "Class".

- Right-click, and "inspect it" from the menu.

- You will get an inspector on "Class class".

- Look at the instance variables for class Class in the inspector,
and have a look at "subclasses".

- You will see that ProtoObject class is a subclass of class Class.

If you look at ProtoObject in a browser, you see it as the root of the
entire hierarchy of classes. But that connection you saw in the inspector
on "Class class" provides hidden magic.

Dave
 
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Object new

cdavidshaffer
In reply to this post by John McCulloch
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!

David


> On Jun 27, 2019, at 8:01 AM, John McCulloch <[hidden email]> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

Class - Metaclass Structure.pdf (17K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Object new

John McCulloch

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' superclass' instance. 

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!

David


On Jun 27, 2019, at 8:01 AM, John McCulloch [hidden email] 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

      
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Object new

cdavidshaffer


On Jun 28, 2019, at 8:16 AM, John McCulloch <[hidden email]> wrote:

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? 


Yes!  Although it seldom matters unless you’re manipulating classes beyond creating instances (or using class-side methods as utilities).  So interesting messages like #superclass and #subclasses are not visible in the class-side of the browser but if you’re using those you’re probably pretty comfortable with looking at ClassDescription etc.

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' superclass' instance. 


The hierarchy’s are parallel except in rare cases where a class has a superclass of nil (like ProtoObject):

ProtoObject superclass ====> nil
ProtoObject class superclass ====> Class

Of course if you now follow the superclass chain from Class you end up back at ProtoObject and then nil.  So, in most cases the only important exception to “every class has a superclass (that is a Class)” is ProtoObject (or Object in some Smalltalk’s).


Btw, I think the diagram applies almost exactly to GNU Smalltalk's model.



Good to know.  Doesn’t reflect Pharo or Squeak which both have ProtoObject above Object but its close enough.

Best,

David


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners