Monday 16 November 2015

Hello World

Transcript show: 'Hello  World'

Which means:
On a Transcript object, show the string 'Hello World'

The Transcript object is a display window, logging results and displaying them.

If we send a specific string to a Transcript, with a polite request to show it, the Transcript object will (hopefully) respond appropriately.

   Transcript (the class) is asked to
   'Hello World'

We can ask politely in three different ways in Smalltalk - i.e. we can send three types of message in Smalltalk.

The message can be a single part. Known as a  'Unary' message.

The message can have two parts - a 'Binary' message.

The message can have multiple named parts - a 'keyword' message.

Keyword messages come in paired parts.
A request name or a parameter name - the keyword -
   followed by
an argument (aka a parameter)

The request's (or message's) name has a colon at the end of it.

The argument is an object.  (Or to be pedantically technical, the object's given name.)

Because everything in Smalltalk is an object, the argument (or parameter) can be as simple or complex an object as you like.

It can be as simple as an integer number (as all the integers are objects).

It can be as complicated as a huge agglomeration of other, smaller objects.

Perhaps an object representing a person, with strings for the person's name, and age and date of birth and place of birth, and lists of every type of medicine the person has ever been prescribed, and when, and a set of schedules of when the patient should be taking their current medication, and freeform notes on what the next treatment steps might be, and a running count of the number of face-to-face treatment visits the person has had, and a dictionary of the person's symptoms, and, and, and

You can even create unnamed objects on-the-fly, complete with new code to execute, and hand these over as parameters.

These spontaneous, anonymous objects are known as 'blocks', and they'll become idiomatic later on.

So Transcript show: 'Hello World'
is a keyword message.

Transcript is the object receiving the request.  In this message, Transcript is the receiver.

show: is the keyword.
We can tell it's the keyword in two ways.
It's between the receiver and the argument.
It ends in a colon.

'Hello World' is the argument or parameter.  It's an object.  In this case, a String object.

Keyword messages can have several keywords, e.g.

   firstKeyword: anObjectBeingUsedAsAParameter
   secondKeyword: anotherObjectBeingUsedAsAParameter
   thirdKeyword: yetAnotherObjectBeingUsedAsAParameter .

This message has a receiver, an object which receives the message.
The message has three keywords.
The message has three parameters.

Messages are also known as methods.  Strictly speaking, a message is a request, and a method is how an object responds to the specific request.

For instance, if a boss says 'Jump!', perhaps the response they are looking for is for you to say 'How high?'

The message they have sent you  is 'Jump!'.  Your method of responding to a 'Jump!' request may be to say 'How high?' back.  Perhaps your chosen method of responding to a 'Jump!' message is to leap up into the air.

In any event, we can use the word's 'method' and 'message' somewhat interchangeably.  One thing's message send is another thing's method of responding.

So the method's name in our example is

To write the method's name out in full, we'd place a pair of >'s to separate the end of the name of the Class of objects from the start of the name of the first of the keywords.


The convention is to have object's names and methods begin with a lower-case letter, and have Classes and their methods have names beginning with Upper-case letters.  In the wild, Classes will always have an InitialUpperCaseLetter, but their Class methods often don't.

To execute the code, open up your Smalltalk, e.g. Pharo, Squeak, Dolphin, Cincom, Cuis  Smalltalk.

If you have a two or three button mouse, click the left-button on the window backdrop.
If you have a single button mouse, click on the window backdrop.  (This type of click is called an action-click).

This brings up the World Menu.  Select Transcript.

Action-click again, and select Workspace (which is named Playground, in some Smalltalks).

Type in the code in the Workspace.  Select it.  Right-click on the highlighted text, if you're using two (or three) button mouse.  (If you are using a single button mouse, there will be (a) key(s) to press a the same time)

Then select 'Do it'.

Hello World will appear in your Transcript window.

No comments:

Post a Comment