September 29, 2022

self - what it means and how to use it


The two instance methods initialize and size employ a psuedo-variable, self, in their code. self means "reference the receiver". It is used inside a method, which means that you shouldn't use it in the Workspace of your learning book. When an object receives a message in its protocol, a correspondingly named method is executed. If this method contains self the object 'knows' that this refers to itself.

For example

   "Initialize the receiver with title set to the
   string 'untitled'. Answer the receiver."
   self title: 'untitled'

The receiver, an instance of the class Artwork, on receipt of an initialize message sends itself the keyword message (in this case a setter) title: 'untitled'. This message triggers execution of the title: method, which instructs the receiver to assign the string object 'untitled' to the receiver's instance variable title. Finally the receiver returns itself as the message answer, the default message answer.

Now consider the size method.

   "Answer with a string that indicates the height of
   the receiver in cms multiplied by the width of the
   receiver in cms."
   ^self height printString, ' cm ',' x ',self width
   printString, ' cm'

Here we have two references to self, but they still mean the same thing, the receiver of the size message that caused this size method to be executed. Smalltalk parses the ^ and will answer the object returned from evaluation of the expression that follows it. In other words it will evaluate

self height printString, ' cm ',' x ',self width printString, ' cm'

What type of object will be returned? The answer is a string object. We haven't yet dealt with the order in which Smalltalk evaluates messages and I will look at this in some detail later. For the purposes of discussing self, it will suffice for you to see that in this case the receiver sends itself both the height and width messages. Both of these return a number object. These receive the message printString which is inherited from the Object class. Sending printString to any object returns a string object as the message answer. Now it's a case of joining, or concatenating, several strings. The class String has a message selector, concatenate: , which takes as its argument a string object.

We could write

'207' concatenate: ' cm'

meaning that the string object '207' receives a keyword message consisting of the keyword selector concatenate: which takes as its argument the string object ' cm'. Note two things here. '207' is a string object, an instance of the class ByteString, it is quite different from 207, a number object that is an instance of the class SmallInteger. Furthermore, ' cm' has a different state compared with the string object 'cm' or ' cm ', watch those spaces!

Smalltalk has a 'shortcut' for concatenate:, you can simply use , (comma).

The size method joins several strings and returns a new string object as its message answer. Sending the message size to an instance of Artwork will result in something like '207 cm x 182 cm' as a message answer.

It's time to look at another form of method, the class method.

Next page » Class methods

Previous page « Artwork instance methods
































































Up to top of page