1

I am investigating Squeak Smalltalk. Somehow I defined an object that initializes and prints the initial value (the instructions for that were in homework). Then I had to define a method (getName), which I did, but I don't know what to do to call the method in the workspace.

To test initialization i used

a := Animal new.
Transcript show: a; cr.

But for anything more than that I just don't know what to do. I tried a getName and a.getName, and more. What is the right way?

Please, help! I don't even know what to Google.

Euan M
  • 1,126
  • 11
  • 20
  • To help for googling, remember that Squeak is an environment for the Smalltalk language. You are dealing with objects and the Smalltalk term for "method" is "message". – Seki Nov 22 '15 at 16:22

2 Answers2

1

Ok, i think i got it. It's indeed a getName, but such line has nothing to do with return value. So i used Transcript show: a getName; cr. And it worked! Transcript appears to be an object too, and : is the way to pass arguments.

Seki
  • 11,135
  • 7
  • 46
  • 70
  • 1
    Not actually: the colon is part of the `show:` message (= method name). It denotes a message that takes an argument (in your case, the result of `a getName`). Look at this [introduction](http://wiki.squeak.org/squeak/3290) to have some beginner help. – Seki Nov 22 '15 at 16:16
  • Yes, `Transcript` **is** an object. It receives the `show:` keyword. This keyword takes an argument. (Any message name ending in **:** is one which takes an argument). `a getName` is returning an object. It returns a string representing the `name` of `a`. `Transcript show:` then shows the `name` of `a`. Again, there are *very* strong conventions in Smalltalk **not** to call your getter "getName" but to call it "name". If you want others to read and understand your code. If your lecturer is getting you to call your getter "getName" he is leading you astray from the Smalltalk way. – Euan M Nov 23 '15 at 18:40
  • `a getName` returns a value. Keyword messages pass arguments. The name of a keyword message ends in **`:`** – Euan M Nov 23 '15 at 18:50
  • 1
    I assume that what you are doing is typing 'a getName' in a Workspace, then selecting 'do it' from the right-mouse menu or keyboard shortcut. When you 'do it', the message is sent, but nothing is done with the return value, so you don't see anything happen. However, if you 'print it' (also in the menu), the value will be printed immediately after the expression; you don't need to send it to the Transcript. – Paul Richter Nov 24 '15 at 13:41
1

In Smalltalk, you send a message to a receiver.

e.g.
Animal new sends the new message to Class Animal.
This is an example of a unary message.

Transcript show: a sends the show: message to Class Transcript, with an argument of a.
This is an example of a keyword message.

Transcript cr sends the message cr to Class Transcript.
This is another example of a unary message.

Transcript show: a ; cr .
This is an example of a message cascade, where several messages in a row are sent to the same receiver.

In a message cascade, you only type the name of the receiver once, use ; to separate the remainders of each message.

Transcript show: a ; cr .

In Smalltalk, keywords which have an argument must have a colon suffix.

The convention is that simple accessors use the same name as the instance variable they access; and an instance of Class Object will have a variable name of the form anObject.

So an instance variable named name would have a getter called name and a setter called name:

Conventionally, we'd then have:

anAnimal := Animal new.
Transcript show: anAnimal name ;
           cr .

Here, we send the name message to anAnimal. It returns the name of anAnimal. As a unary message, it has higher precedence than the keyword message Transcript show: <something>, and so is evaluated first. The return from the anAnimal name message becomes the argument to the Transcript show: <something> message.

You can see this for yourself. In the Workspace, highlight anAnimal name and then click and choose 'Inspect it'. This will bring up an Inspector window, and it will show you the object returned by the anAnimal name message.

These answers may help you understand:
Explain a piece of Smalltalk code
Keyword messages in smalltalk

This article, Beginning to Smalltalk: Hello World, goes into it in a little more detail, using Transcript show: 'Hello World'.

Community
  • 1
  • 1
Euan M
  • 1,126
  • 11
  • 20