In this session we'll see the Pharo object model.
We'll take an overview to show you its elegance and simplicity.
You don't need to understand everything now
because we'll go over these notions over the next few weeks.
In Pharo there are only objects and messages.
There are lots of objects,
to represent the mouse pointer, booleans, arrays,
numbers, strings, windows, scrollbars and so on...
Even compilers,
system objects such as sockets, fonts, collections and so on.
All these are objects
to which we can send messages.
There are lots of different messages.
You can send size to a collection to get its size.
There are the messages plus, at:put:, do: and so on.
Objects and messages.
The messages indicate the programmer's intention.
When I send a message to an object, I'm asking it to do something.
It's really a request made to the object.
And the object decides what method to use.
This is the how. How to achieve the programmer's intention
by deciding what method to employ.
Next we have another concept, which is that of closure.
These are lexical closures, blocks,
sorts of anonymous methods in Pharo.
We call them blocks. This is important vocabulary.
Blocks are delimited by square brackets, as you see here.
So all this is a block.
So you should really look at this as an anonymous method.
In Pharo we have a very simple and uniform model.
Everything is an object, an instance of a class.
Even classes and messages are objects too.
And all computations between objects are done via message passing.
We really talk about sending a message,
that's the term, and not about executing a method,
because when you send a message to an object,
there's a particular algorithm called the method lookup,
just one algorithm,
which selects the right method to use.
The methods are virtually bound.
They accept late binding.
I send a message to an object,
and the method lookup selects the right method.
We'll come back to that in a dedicated session.
The Pharo object model is as follows.
Instance variables are protected.
All objects have instance variables and they're protected.
They're private to the object
or are accessible from subclasses.
The methods are public and virtually bound.
All the methods are public in Pharo.
And Pharo accepts single inheritance between classes.
A class can only have one superclass.
Here's an example of code, the cross-product of two points.
There's point1 and point2.
I multiply the x field of point1 by the y field of point2,
and subtract the multiplication
of the y field of point1 and the x field of point2.
It's an example of a computation you can do in Pharo.
To create objects we have special messages.
Here, for example,
if I send the message @ to the integer 10
with the argument 20,
it'll a create a Point, an instance of the class Point.
The name of the message is @,
the integer that received the message is 10,
the one before the name of the message,
and the argument, after the name of the message, is 20.
Here's another example.
I want to create a string of characters like this one here.
How is this string obtained?
We've used a first chain here, Pharo,
we've sent it the message comma,
as argument we have the string is cool...
Which is here, right?
The meaning of the message comma is the concatenation of strings.
The two strings are glued together to produce a single string.
We can create objects directly
with the message new.
I send new to the class Monster and I'll get an instance of the class:
aMonster.
I can create an instance of the class Array,
by specifying new
and attributing a parameter, which I do with a colon,
and then here the integer 6.
And I get an array of size 6.
We can have dedicated messages to create objects,
and I can define my own messages to a class.
I could create a class Tomagoshi,
with a method withHunger
and for this method I'll give the integer 10.
This creates an instance of the class Tomagoshi
by attributing a parameter.
So these are specialised messages for object creation.
In this lecture we've seen an overview of the Pharo object model.
You don't have to understand it all, we'll come back to these notions.
But it's important to remember that there are no constructors in Pharo,
no static methods, type declarations, interfaces,
package/private/protected modifiers...
All those classic modifiers you get in object languages.
There are no parametrized types, no boxing/unboxing and so on.
And you'll see that it's a really powerful language.
So, in Pharo, everything is an object.
Computation is done via messages sent to objects.
Methods are late bound.
There's an algorithm, the method lookup,
which selects the right method when an object receives a message.
There's the notion of blocks, which are anonymous methods.
Very important in Pharo.
And we can send particular messages to classes
to create objects.