Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

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.

So
   Transcript (the class) is asked to
   show:
   '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.

theAuthorsPetExampleOfAnObject 
   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
firstKeyword:secondKeyword:thirdKeyword:

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.

AuthorsPetExampleOfAnObject>>firstKeyword:secondKeyword:thirdKeyword:

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.




Beginning to Smalltalk - A Beginner's Smalltalk Tutorial series

I'm hoping this blog will become an ordered sequence of tutorial posts.  They're intended for people just beginning Smalltalk.  Perhaps even beginning to learn how to develop applications for desktop computers.

Smalltalk is wonderfully cross-platform.  I write my own applications on Windows on a PC, and on Linux on a Raspberry Pi.  As I write them, I am completely relaxed that they can be used by people who prefer Apple Macs, and it's perfectly possible to write applications that run in a web-browser.

I'm personally working on writing Smalltalk applications that manage, control and distribute multi-platform mobile apps written in JavaScript.

I've been making posts for myself in the Smalltalk in Small Steps blog.  Generally little 'aha' moments, or "I really must keep that filed for later when I need it" posts.

My plan is now to tidy up those posts, put them into a sensible sequence, and verify they are useful on several different Smalltalk platforms.

I hope they'll become more generally useful to the Smalltalk community.  Most importantly, I hope they'll become useful to the potentially much larger audience of people who don't know much, if anything about Smalltalk, and who are interested in knowing more.

If this blog is useful to you, I'd be very interested to hear from you about your own plans for learning Smalltalk - why you want to learn it, what you want to do with it, and how you get on with learning it.

It's a fabulous learner's language - and once you know Smalltalk, it makes it easier to understand the deeper patterns of object-oriented work you might do in Java, Objective C, Ruby or Python.

Good luck!

My email is euanmee at a well known email service run by Google - gmail.com