Hello everyone.
In this class, I'll illustrate Pharo
by comparing it to another OOP language, Java.
In Pharo, you only manipulate objects.
You do things thanks to messages only
and by assigning values to variables.
The following slides will show a few equivalences between Java and Pharo
to understand their differences and similitudes.
Over there,
Java is used to create a new collection
which contains strings.
This collection is affected to a variable called strings.
In Pharo, it's the same.
The class which manages collections
is called OrderedCollection, not ArrayList,
but the behavior is the same.
You have to be aware of several things:
first, in Pharo, new is a message sent to a class.
The message new is sent to the class OrderedCollection
to create a new instance of the collection.
In Java, new comes first.
This syntactic collection is specific
to object instantiation in Java,
whereas in Pharo it's standard message sending.
There's no static typing in Pharo.
Many things which are needed in Java are not used in Pharo.
You don't need to specify that the collection contains strings.
And strings isn't a collection.
There's another equivalence when it comes to creating a child process.
We'd like to execute this bit of code
in another process.
In Java, you must create
a new instance of the class Thread
by giving it Runnable() as a parameter.
You write new Runnable().
Runnable() being an abstract class, you have to define the missing method.
This method is called run().
It contains the code you want to execute in a child process:
this.doSomething().
The method doSomething belongs to the same class
as that where the code is located.
In Pharo, the syntax is much simpler.
What is equivalent to all that part is this part.
In the end,
this start helps to launch the child process.
You can do this thanks to fork,
a message which gets sent to the block.
Since Java 8, the latest version of Java,
the syntax of Java has improved much.
As you can see, code in Java is much clearer.
The equivalent of this block
is the lexical closure located here.
The rest is the same as in the previous slide.
In Pharo, there are three types of messages:
unary messages,
which are messages without arguments applied to a receiver,
such as the method factorial we already studied
or as the method cr
which displays an empty line in the console.
Binary messages are applied to a receiver
and take a value as an argument.
For instance, operators
such as + which adds three and four.
-> is used to create a pair with a key and a value.
5->10 creates the pair 5 10.
The third type of messages is keyword-based.
They can take one, two, three, or more parameters.
They are separated by a colon.
The keyword message here is show. It gets sent to the object Transcript
and takes the string in the console as a parameter.
In the second example, the message is between: and:.
It's one message separated in two keywords.
It takes two arguments and is applied to one receiver.
The message between: and: is applied to the object 2
and takes 0 and 5 as arguments.
To compare with Java,
especially when it comes to keyword-based messages,
as they are very different compared to Java:
In Java, you have one receiver, one period,
the name of the message sent to the object,
and the arguments between parentheses separated by commas.
In Pharo, you express the same thing
by separating the keywords from the name of the method
and by putting the arguments in the middle.
For instance:
I send the message send to a postman
in order to send mail to a recipient.
This is the Java syntax.
I'll decompose it gradually to end up with the Pharo syntax.
First step:
extract the parts that belong to the Java syntax:
the period, the parentheses, the comma, and the semicolon.
Let's remove them.
You end up with: postman send mail recipient.
If you turn this into a sentence to make reading easier,
you get: postman send mail to recipient. It's easier to read.
To get the right Pharo syntax, you add colons.
There's one message, the message send: to: applied to the postman.
It takes two arguments, the mail and the recipient.
Conditionals such as if and else,
and loops such as do, for, etc.
are keywords in Java. In Pharo, they aren't.
They aren't specific words defined by the language and the compiler.
They're just methods and messages sent to some types of objects.
In Pharo, for instance, to execute if,
you send the message ifTrue: if False:.
It's a message you send to one Boolean.
It takes two blocks as parameters: one is executed if the Boolean is true,
one if the Boolean is false.
Likewise, loops are just methods sent to some types of objects.
In the first example,
the object 4 is a number.
I can send the message timesRepeat: with a block as a parameter.
The underlying method executes the block
as many times as is indicated by the receiver.
4timesRepeat: executes something four times.
Likewise, the message to: do: is sent to a number.
It takes a number and a block as parameters.
The value i which is the parameter of the block
takes every value between 0 and 100.
Likewise,
the message to: by: do:
selects the values comprised between 0 and 100
which are multiples of 3: 0, 3, 6, 9, etc.
Finally, the message do: sent to a collection
is the same as a foreach loop in Java.
It executes the block for each element of the collection.
The variable :each
first takes the first value of the collection,
its first element.
The block is executed using this value of :each.
Then :each takes the second element of the collection.
The block is executed using this value until the end of the collection.
To sum up, there are three types of messages:
unary messages, binary messages, and keywords.
The order of precedence:
first execute what's between parentheses,
then unary messages, then binary messages, then keywords.
Conditionals and loops
are messages sent to some types of objects,
unlike as in other languages such as Java,
where specific keywords of the language
have specific instructions regarding the compiler.
In Pharo, they're messages sent to specific objects.