This will be an overview of the syntax.
We'll show you the entirety of the syntax...
Don't get stressed if you don't understand it completely.
We'll go over it all again.
The idea is to show you all there is in Pharo.
We're going to look at the main syntactical elements.
We'll see that there are 3 kinds of messages.
And well look at block syntax,
an essential part of Pharo.
Don't worry if you don't get it all, we'll repeat all the points.
Just to remind you, you have all the syntax on this slide.
I can go into the details a bit now.
Here's a method definition, exampleWithNumber.
And it has an argument.
I have local variable definitions with vertical bars.
I have the return, which lets me give a value.
I have loops, an affectation...
The period, for example, is an instruction separator.
We'll look at all this.
We'll start with the traditional Hello World.
Here, if you like...
You can execute all instructions in Pharo yourself
to see what the result is.
So here I have a string,
which I convert into a morph, a graphic element,
and I ask it to open in a window.
So I send asMorph to the string object.
And I send openInWindow
to the resulting morph.
So we have two message sends and one string object.
A more complicated example.
Imagine you want to take the Pharo logo from the web.
Well, you do it this way.
You do ZnEasy getPng: with a string which represents the URL
of the resource you want to read.
And once again you convert it into a graphic object
and ask it to open in a window.
So we see that ZnEasy is a class
because classes start with a capital.
The colon of the message getPng: means I'm expecting an argument,
which is the string http://pharo.org.
This string is the argument of the method getPng:
The messages asMorph and openInWindow
are read from left to right.
So you can cut'n'paste this code
or retype it in Pharo,
and you'll get the Pharo logo.
So what syntactical elements are there in Pharo?
You have comments, which start with double quotation marks.
That's a comment.
Characters are always preceded by a dollar sign.
$A is the character A.
Strings start and finish with a single quotation mark.
You have symbols, which are unique strings.
These begin like this, with the hashtag.
#a is the symbol a.
There are literal arrays.
Here I have one with 12, 23 and 36. Three elements.
Integers have various representations,
but we won't dwell on this.
We usually use representations in base 10.
But if you want one in base 2, that's OK.
You have reals. This is the same.
You can find the syntax if you need.
You have booleans, with true and false.
You have undefined.
The null of Java is nil in Pharo.
It's an instance of the class UndefinedObject.
And you have points.
This is described in Pharo under the form
of 10 @ 200, for example.
So I'm creating the point 10, which is 10 on the x,
and 200 on the y.
So we've seen the essential elements for the syntax.
Now we have the constructs of the language.
You have temporary variable declaration.
It's a vertical bar followed by another vertical bar.
Assignment is a colon and an equal sign.
In the variable var I put aValue.
I have messages in my system.
How do I separate them?
With a period.
When I want a sequence of messages.
There's return,
where a function or method gives a value.
I use the little hat, which is called caret in English.
Then you have blocks,
which, technically speaking, are lexical closures,
and which you can take as anonymous methods.
I'll show you what a block is.
It's defined with square brackets.
And it's executed with the message value.
We'll look at this.
There you have all the constructs.
So what is the essence of a Pharo program?
It's creating objects by using messages,
sending messages to these objects,
and, now and then, using blocks, or anonymous methods.
That's all.
There are three types of message in Pharo. Why?
In order to minimize the number of parentheses used.
It's fairly simple.
A unary message is always a receiver and a selector.
Here for example I've said that I want 9 squared.
I want today's date.
I send the message today to the class Date.
There's no argument.
Then there's the binary message,
which also takes the form of a receiver, selector and argument.
But the selector will be
plus, minus, tilde, equals, slash... That sort of thing.
All those mathematical signs are for binary messages.
Here we see 1+2. This is a message.
Sending message + to object 1 with argument object 2.
@ for the creation of a point is also a message,
a binary message.
Here I create the point 3@4
by sending the message @.
Then you have what we call keyword messages.
A keyword message takes the form
of receiver key1: arg1 key2: arg2.
So the example is...
is 2 between 10 and 20
If you wrote it in Java or C it would be like this...
You'd write betweenAnd...
Then, between brackets, you'd write (10, 20).
In Pharo you express it in the form of key and key.
We'll look at this again, but it gives you an idea of the syntax.
There's a precedence between messages,
the system will execute in priority the parentheses,
then the unary, binary and keyword messages.
Why?
To avoid writing too many parentheses.
We could have had a system with parentheses all over the place,
but the code would be illegible.
I'll go back over each type of message in a simple way.
This unary message, 10000 factorial.
Try it, it's a very big number.
I send the message factorial to the object 10000.
There's no argument, it's a unary message.
Similarly, a binary message. 1+3 is a message, a binary message.
The selector, the name of the method, is +.
I send the message + to the object 1 with the object 3 as argument.
A keyword message, as I explained earlier...
I'll just tell you again. Teaching is all about repetition.
I have a keyword message with keyword and keyword,
which is the message, with the arguments,
which are floating inside the message.
The idea is to have a syntax that children can use
and to have something close to English.
So you can introduce the arguments inside your messages.
As an example, imagine I'm sending an HTTP request.
ZnClient is a Pharo HTTP server.
So I create an instance of Zn server.
So new is a unary message, there's no argument, no colon.
url is a keyword message, there's a colon.
queryAt: put: is another message.
It's another keyword message.
And get is a unary message.
What we can also see in this example are semi-colons.
We call this a cascade,
which sends all the messages to the same object.
You don't need a temporary variable.
And you can write more compact code.
You have to understand that messages are everywhere.
Conditionals, loops, iterators and concurrency are expressed as messages.
My explanation about those three messages
applies across the board.
It's the cornerstone of the system.
There's nothing other than these messages.
Let's have a look.
I've taken the silly factorial definition here.
How do I define it?
I define my method factorial.
I put a comment for the method. Double quotation marks.
And I start with self.
If the object getting the message is zero,
then I'll give 1.
If self is positive,
for example, factorial 6, self equals 6...
If this is true than what will I give?
6 multiplied by 6 minus 1 factorial.
The recursive definition we all know. And that's that.
And the interesting thing to note here
is that ifTrue:, which is a condition,
is also a message, a keyword message with a colon,
sent to a boolean.
You also have ifFalse:ifTrue:, ifTrue:ifFalse:...
All these conditions are messages
which satisfy the rules,
and are keyword messages like I presented earlier.
So conditions are messages.
Loops are messages too.
Here I'll make a loop from 1 to 4.
From 1 to 4, what do I do?
A block. An anonymous method.
Here I'm saying I want it to show the value of i,
and i will take the value of 1, 2, 3, 4...
And there you can see that to: do:
is a keyword message.
It's a message that takes the form of a loop.
There are other loops in Pharo: timesRepeat:, to:by:do: and so on.
We'll look at them all.
But, again, these are messages...
So here, to: do: is sent to object 1 with 4 as parameter and a block.
We'll see what a block is very soon.
You also have iterators.
Iterators are different.
I send the message do:
to the collection,
saying what I want to apply to each of the elements.
So the each, the argument of my function,
will take as value 1, 2, -4 and -86.
And what I do here is that the each...
I apply the absolute value and display it.
Once again, this will be another course.
We'll explain iterators.
Iterators are something you get in Java 8.
And they're part of the core of Pharo.
So what is a block?
Blocks look like functions.
If in maths you wrote f(x) = x²+3,
you'd write it like this, literally.
I define a variable in which I affect a block.
A block is something inside square brackets.
Here, the block has an argument which is x.
And I put a core, which is x+3.
And when I want to execute this block...
The application of the function.
That's the definition of the function, now I have the application.
I send the message value: to the block. I put value: 2.
x is 2, so my block will give me 5.
OK?
So what's a block? It's a kind of anonymous function, it has no name.
In fact, it's a lexical closure.
We'll explain that.
Blocks are real objects.
They can be passed as method arguments, stored in variables,
returned...
So they can be used as button triggers,
as callbacks in web applications... These can be blocks.
We've already seen block usage.
In iterators you have blocks.
We saw blocks were used in conditions.
There'll be a course about blocks.
It's elegant, it was introduced in Eiffel,
it was recently introduced in Java 8,
there was a first version in C#...
So it's fundamental.
Blocks are a fundamental element
of Pharo's syntax and computational model.
How do we define a class in Pharo?
The browser you see here shows you packages.
You have the classes inside.
And it shows you a template.
The template is saying that by default you inherit an object,
and the name of the class.
We'll see it for the class Point, for example.
So the definition of the class...
I have the class object. It's also a message!
The message subclass: InstanceVariableNames:
blah blah blah...
With...
I define the class Point,
and I give the instance variables x and y.
That's just to show the syntax always follows the same model with messages.
You don't usually write it out by hand.
The tool gives you the template and you just fill it in.
So how do we define a method?
As I said, the methods are public and virtual,
and by default return self.
So, when you're in the browser,
it suggests a template,
the name of the potential method and a comment...
We're nice and polite so we comment on our code.
We can also put temporary variable names and statements.
What does this gives us? We've seen the method factorial.
Here I'm in the class Integer.
I look at the method factorial, I have its code,
with the name of the method, its comment
and the core of the method that I've defined.
So, to sum up, what have we seen?
There are 3 kinds of messages: unary messages, without arguments,
binary messages, where the selectors are mathematical,
and keyword messages, where there can be several arguments.
There's a priority: parentheses first,
then unary, binary and keyword.
And if there are same-level messages it's from left to right.
What have we seen? The syntax is very compact.
There are few constructs but they're really expressive.
It's mainly messages and lexical closures, or blocks.
There are three kinds of messages.
You'll do an exercise to develop a language.
Like that you can define what we call Domain Specific Languages.