kenyan programmer

mostly about programming


Pharo Smalltalk is like English

“Image showing progression from java to pharo syntax”

I’m currently on week 2 of the pharo mooc, and it’s fun and eye opening so far.

Smalltalk the language was designed by Alan Kay to be easy enough for a child to use in his dynabook project, and that is why it has such a small syntax.

In the mooc, an example I see of this is when it comes to your typical method call in C-like languages.

Let’s say we have a postman object that we use to send mail to a recipient. It can be represented as:

postman.send(mail, recipient);

In smalltalk in the place of calling an object’s methods, we think in terms of sending messages to objects. In the example above, we would be sending the message send to the postman object with parameters mail and recipient.

To gradually convert the method call above to its pharo equivalent, we would do the following:

  • Separate the words from the structure

    postman . send ( mail , recipient );

  • Remove the structural tokens, leaving only the words

    postman send mail recipient

  • Make it more English-like

    postman send mail to recipient

  • Convert to pharo’s message sending syntax

    postman send: mail to: recipient

In this example, we’re sending a keyword message send:to: to the postman object.

In pharo there are 3 different types of messages:

  • Unary messages that have no arguments. For example, to square an integer, we send the squared message to it.
5 squared
> 25
  • Binary messages that take a single argument. Common arithmetic is implemented like this. In the example below, we add 1 and 2 by sending the binary message + to the object 1 with the argument 2.
1 + 2
> 3
  • One or more keyword messages that are followed by a colon. For example, below we check the whether 2 is between 1 and 3 by sending the keyword messages between:and: to integer object 2 with arguments 1 and 3.
2 between: 1 and: 3
> true