Hello. This week, we'll get to the heart of the matter,
about syntax especially.
This class will be mostly about dealing with the messages of Pharo's syntax,
which is most of Pharo.
In Pharo, there are three main things:
objects, messages, and closures called blocks.
There'll be a course about blocks.
You can cover almost the entire syntax of Pharo with this.
Pharo's syntax stems from that of Smalltalk
which was designed to teach children how to program.
It's about writing programs which are like short sentences
a child might write.
It's also about minimizing the number of parentheses.
We'll see how it works.
For instance, let's take a tile on openstreetmap.
I want to open it in a graphic window.
This is an example of the syntax.
I use parentheses because I want to get a png file first.
I convert the png file into a Pharo graphic object
before opening it in a window: do it with Pharo and you'll get the result.
It'll probably be water as there are many water tiles.
In Pharo's syntax, there are three types of messages
to minimize the number of parentheses.
There are unary, binary, and keyword messages.
Let's see about them.
With unary messages, you have one receiver then the method's name.
With binary messages, one receiver, the selector, then an argument.
With keyword messages, one receiver, then one argument or arguments.
You can distinguish them thanks to the colon here.
We'll study this.
What is the use of these three types of messages?
You can differentiate them.
It changes the way programs are executed.
You always execute what's between parentheses first;
then unary messages; then binary; then keywords.
When at the same level, you use the same order,
from left to right.
Let's see about unary messages:
if I ask small integer 1 what its class is, it'll return SmallInteger.
This will be your homework for next week:
if I send the message not to the Boolean false, I get the Boolean true.
If I query the class Date for today's date,
it'll return 24 May 2009, for instance, which is a Date Object.
If I send the message pi to the class Float,
it'll return a representation of the object pi.
I don't know whether you noticed,
but I sent messages to objects
as well as messages to classes in the exact same way.
I didn't say: "It's a class. Careful, you have to do something."
I send the message class to the small integer, it returns the class.
I send the message today to the class Date, it returns a date.
Nothing fancy.
In Pharo, classes are also objects.
Messages sent to classes are processed like messages to instances.
Which makes complete sense.
I have an example for you.
This exercise isn't related to syntax. It's just to have fun.
I thought it would be fun to determine what are all the methods
implemented in the class Point. It's a unary message. I can say:
"class Point, return all your methods and the message names you understand."
It'll return a long list with all the messages.
See how easily you can build tools?
If I put this in a list,
I'll get a list with all the messages the class returns.
That's how the browser you'll use works.
Let's refine this: I'd like to query the system
for all the unary selectors of the class Point.
I write it like this: "Class Point, list all your selectors."
It'll return an array, a collection.
I write that I want to select...
This is a keyword message.
I want to select among the selectors those which are unary.
There's a query called #isUnary.
It's a message about method names which selects such things.
This is a small footnote in the syntax.
You'll learn about it in the course about iterators, select: being one.
It's very elegant: I can query the system in one line.
Back to the subject at hand.
There are binary messages: they are composed of anObject,
aBinarySelector, anArgument.
Binary selectors are a combination of these incomprehensible things:
%, |...
What can't be understood is binary. What looks like math is binary.
For instance, I send the message + to the object 1
with the object 2 as argument. It returns 3, which is great.
> is a binary message.
@ is also a binary message (it creates points).
String concatenation with a comma is a binary message.
I write: 'Black chocolate','is good'
I get the string 'Black chocolate is good'. Which is true.
About keyword messages...
People often find them frustrating
when they're used to a Java-like or C-like syntax,
with a period, parentheses, and a comma in the middle.
But we do it like this: in the middle.
What you have to see in this example is that the message is:
kayword1keyword2.
The message is full. It's composed of two parts.
It's time to test yourself. I'll give you a couple seconds.
What are these messages? What types do they belong to?
The first has no argument: it must be unary.
The second too.
There's a colon: it must be a keyword.
There's something with a comma I don't understand: it's binary.
It's weird: it's binary. There's no argument: it's unary.
There's a colon: it's a keyword.
It might seem difficult, but...
This is the solution.
For example,
let's imagine I want to change the x value of a point.
This is a point. It's a binary message.
I write setX: with an argument.
It returns a point in which 2 has been changed.
It doesn't look so hard, does it?
Now with the message at:put:. I have an array.
It contains strings: 'Calvin' 'hates' 'Suzie'.
I want to change the second element.
I write at: 2 put: 'loves'.
This is 2. So 'loves' comes here.
About arrays, you can see
the method is called at:put:.
This method changes the value of a collection at a given index.
As you can see, collections start at 1 in Pharo, not at 0.
I want the second element, so I count: 1, 2.
You don't have to subtract anything.
I'll give another example of keyword messages:
when I ask whether 12, which is an integer,
is between 10 and 20,
it's the message between: and:.
In the syntax, it would be the same as: 12.between
And(10,20).
I think I've focused on this enough.
There are three types of messages:
unary, binary, and keywords.
That's all.