Please explain self

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

Please explain self

Pharo Smalltalk Users mailing list

I understand if you have Class A with methods p and q that you can call q from p doing:

self q

But there are some cases where a method doesn’t exist but self is still used. I found a tutorial on creating a Reddit type app and it had a .mcz which I loaded.

Here is an example from Class StLoggedInComponent and method refreshReport:

self report rows: (self session user tasks select: self filterBlock).

There is a method ‘report’ so I understand that, but there is no method named ‘session’ so what is the use of self?

And Class StDatabase has method ‘updateTask:’ has:

self subclassResponsibility.

But no method called ‘subclassResponsibility’ exists in the Class.

Also, Class StLoggedInComponents has method ‘testTasks’ with this:

^ self class testTasks

What is ‘self class’?

Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: Please explain self

Davide Grandi-3

Objects are instances of a class.
Classes are objects, so they have methods.
'self class' returns the class of self.

If a class doesn't have a methods ...
... the search continues in the superclass.
(every class has a superclass ... apart from Object).

If you download the Blue Book  - "Smalltalk-80 - The language and it's implementation",
it's free on the net, you'll find an AWSOME explanation and diagram from page 270 on, until ...
... the final map : classes and metaclass.

but you've to read it these pages, before. There's a 'metaclass' concept to acquire.

Bon voyage !

    Davide

On 12/12/2020 20:20, g_patrickb--- via Pharo-users wrote:

I understand if you have Class A with methods p and q that you can call q from p doing:

self q

But there are some cases where a method doesn’t exist but self is still used. I found a tutorial on creating a Reddit type app and it had a .mcz which I loaded.

Here is an example from Class StLoggedInComponent and method refreshReport:

self report rows: (self session user tasks select: self filterBlock).

There is a method ‘report’ so I understand that, but there is no method named ‘session’ so what is the use of self?

And Class StDatabase has method ‘updateTask:’ has:

self subclassResponsibility.

But no method called ‘subclassResponsibility’ exists in the Class.

Also, Class StLoggedInComponents has method ‘testTasks’ with this:

^ self class testTasks

What is ‘self class’?

Thank you.

-- 
Ing. Davide Grandi
linkedin : http://linkedin.com/in/davidegrandi
Reply | Threaded
Open this post in threaded view
|

Re: Please explain self

Richard Sargent
Administrator
I'll add a small bit to Davide's excellent answer, by way of connecting some of the dots. In particular, the browser makes a class look like one thing and we talk about instance side and class side.

The reality is that the browser presents either the class' behaviours or the metaclass' behaviours, depending on whether we have chosen to see "the instance side" or "the class side". Admittedly, the names we use for the sides add to the possible confusion.


On Sat, Dec 12, 2020, 12:20 Davide Grandi <[hidden email]> wrote:

Objects are instances of a class.
Classes are objects, so they have methods.
'self class' returns the class of self.

If a class doesn't have a methods ...
... the search continues in the superclass.
(every class has a superclass ... apart from Object).

If you download the Blue Book  - "Smalltalk-80 - The language and it's implementation",
it's free on the net, you'll find an AWSOME explanation and diagram from page 270 on, until ...
... the final map : classes and metaclass.

but you've to read it these pages, before. There's a 'metaclass' concept to acquire.

Bon voyage !

    Davide

On 12/12/2020 20:20, g_patrickb--- via Pharo-users wrote:

I understand if you have Class A with methods p and q that you can call q from p doing:

self q

But there are some cases where a method doesn’t exist but self is still used. I found a tutorial on creating a Reddit type app and it had a .mcz which I loaded.

Here is an example from Class StLoggedInComponent and method refreshReport:

self report rows: (self session user tasks select: self filterBlock).

There is a method ‘report’ so I understand that, but there is no method named ‘session’ so what is the use of self?

And Class StDatabase has method ‘updateTask:’ has:

self subclassResponsibility.

But no method called ‘subclassResponsibility’ exists in the Class.

Also, Class StLoggedInComponents has method ‘testTasks’ with this:

^ self class testTasks

What is ‘self class’?

Thank you.

-- 
Ing. Davide Grandi
linkedin : http://linkedin.com/in/davidegrandi

mjldpppiomdnfbme.png (42K) Download Attachment
mjldpppiomdnfbme.png (42K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Please explain self

Richard O'Keefe
In reply to this post by Pharo Smalltalk Users mailing list
#subclassResponsibility is defined in Object.
A good way to find this out is to
 - select 'subclassResponsibility' with the mouse
 - type Control-M
Control-M asks for a list of the iMplementors of the method.
Control-N asks for a list of the seNders of the message.

This particular method just raises an exception.
It is how you define what some languages call an "abstract method";
a method whose *interface* is defined in the current class
but whose *implementation* is deferred to subclasses.

I can't help you with #session except to suggest that it is
probably defined in some superclass.  In the browser button bar
between the upper and lower panes you may see
(*) Flat ( ) Hier
Clicking on Hier will change the class pane (2nd from the left,
upper level) to an indented list, making it easy to see what
the superclass chain is.

On Sun, 13 Dec 2020 at 08:21, g_patrickb--- via Pharo-users <[hidden email]> wrote:

I understand if you have Class A with methods p and q that you can call q from p doing:

self q

But there are some cases where a method doesn’t exist but self is still used. I found a tutorial on creating a Reddit type app and it had a .mcz which I loaded.

Here is an example from Class StLoggedInComponent and method refreshReport:

self report rows: (self session user tasks select: self filterBlock).

There is a method ‘report’ so I understand that, but there is no method named ‘session’ so what is the use of self?

And Class StDatabase has method ‘updateTask:’ has:

self subclassResponsibility.

But no method called ‘subclassResponsibility’ exists in the Class.

Also, Class StLoggedInComponents has method ‘testTasks’ with this:

^ self class testTasks

What is ‘self class’?

Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: Please explain self

Stéphane Ducasse
In reply to this post by Pharo Smalltalk Users mailing list
Hello 

All this is explained in the Mooc (with mutliple videos and gradually) 
and Pharo by Example chapter.

Pay attention you do not need to understand the metaclass core to program in Pharo.
This is just for fun and a bonus. This is why in general I do not show this diagram directly. 

Check the mooc videos we designed based on more than 10 years of experience
teaching Pharo to people.

S


On 12 Dec 2020, at 20:20, g_patrickb--- via Pharo-users <[hidden email]> wrote:

I understand if you have Class A with methods p and q that you can call q from p doing:

self q

But there are some cases where a method doesn’t exist but self is still used. I found a tutorial on creating a Reddit type app and it had a .mcz which I loaded.

Here is an example from Class StLoggedInComponent and method refreshReport:

self report rows: (self session user tasks select: self filterBlock).

There is a method ‘report’ so I understand that, but there is no method named ‘session’ so what is the use of self?

And Class StDatabase has method ‘updateTask:’ has:

self subclassResponsibility.

But no method called ‘subclassResponsibility’ exists in the Class.

Also, Class StLoggedInComponents has method ‘testTasks’ with this:

^ self class testTasks

What is ‘self class’?

Thank you.


--------------------------------------------
Stéphane Ducasse
03 59 35 87 52
Assistant: Aurore Dalle 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
Villeneuve d'Ascq 59650
France