Hello. During this course, I will mainly talk about object-oriented programming,
and we will learn about the essence of dispatch
and late binding in objects-oriented languages.
So this course uses Pharo as an example.
It's good that Pharo has been well implemented.
In the next class, we'll talk about its big potential generalization.
Let's start.
Let's look at Booleans.
Booleans in Pharo
are the most basic stuff indeed.
There are eager Boolean operators like &, |, not,
lazy operators like or:, and:, with argument evaluation when necessary;
there are also conditionals which we will see in another course.
So it's well implemented but there's nothing amazing or specific.
Last week, I asked you to do some exercises.
There were three questions.
How to implement Not,
how to implement Or,
and the most important: what is the goal of these exercises?
I will answer the first two questions; then in the next course,
I will answer the last question.
The exercise was really...
If you send the message not to false, it will return true.
If not to true, it will return false.
You have objects. How to implement this?
I'll first give you some hints.
The solution does not use conditionals.
Mostly, you find a solution with a condition.
But I'm telling you that my solution, implemented by Pharo,
does not contain any conditions.
Nor conditionals.
So you don't have if in the solution.
Think about it for a bit to see if you have an idea.
Normally, this kind of hint doesn't really work.
The second hint: the solution uses three classes.
It has the class Boolean
which is abstract.
It has the class True and the class False.
The Boolean object true is the singleton instance of True.
Do you see the difference?
The instance true starts with a lower case
and the class False starts with a capital F.
And false is the singleton instance of False.
If we visualize it in a diagram,
we can see true is instance of True, and false of False.
In theory, with this hint, the solution should be obvious to you.
I am not sure you can see it.
I will let you think more about it.
The solution is in the end...
I won't tell you right now... Let's think about it.
How do you express a choice in object-oriented languages?
A choice is expressed by defining classes
with compatible interfaces, i.e. methods,
and by sending a message to an instance.
That's my example. When I type x open,
I choose the right method associated with x.
It means if it's a file, it will open a file; if a window, a window;
if a tool, a tool.
So the method will be selected based on x's class.
Then how can we have the solution with this hint?
We will implement it like this.
It means I implement the method not in the class False;
in this case, it will return true.
I implement the method not in the class True; it will return false.
In a diagram, it looks like this.
You can see this solution doesn't have any explicit conditions.
I don't use any if in this case.
How does it work?
It works like this. When I send the message not,
where do I search for the method not? In the receiver class.
true is instance of True.
So this method here will be executed,
and the result will be false. It works.
Now I send a message to the instance false.
Where do I look? In the class False.
This not is executed, and it returns true.
I have implemented my Booleans,
the Boolean negation with two methods
without the use of conditionals.
We can also look at the implementation of the superclass.
The Boolean class is abstract.
It has two subclasses which implement the necessary operators.
In Pharo,
you express not as an abstract method on Boolean
by using self subclassResponsibility.
I just wanted to tell you about Pharo to be comprehensive.
By now, you must have understood
how to express the behavior of Or.
I'll give you time to express this.
The idea here is to define a method
which will use an extra argument.
Depending on its value,
it'll be followed by the right result.
You often think using a condition is enough:
No. That's the thing.
Once again, you don't need conditionals to implement Or.
I will give you 10 seconds to think about it.
You were supposed to have prepared this.
I will define Or in the abstract Boolean class
as an abstract method. Very good.
Then in the class False: it's written here.
The receiver belongs to False. What do I return?
When it's true, it returns true: when false, false; when anything, anything.
So it means I return the argument.
So here, the implementation of Or in the class False
is about returning the argument.
That's exactly what we did.
Likewise,
if we look at the class True, it's explained.
It's explained here.
When I send or to the receiver true,
I return the receiver.
So I return true.
As you can see here again, I don't use conditionals,
I just used message sends.
So in a cleaner way, how is it written in Pharo?
We know that true is the receiver of the message;
so instead of writing true, we can write self.
If you read the definition, you can see self and say:
"It makes sense. Since the receiver is true,
"it's exactly the same."
Once again, let's visualize it.
When I send the message or with something
to the object true,
I look for this definition of or here,
and it will return self, hence true.
When I send the message or with something,
I look into the class False which is false.
So I end up with this implementation, and I return alpha.
This is the Boolean table I was trying to implement.
What you have to remember is:
the solution we have implemented does not use any conditionals
or any conditional instructions
such as an explicit loop;
it lets the receiver decide.
It means that I tell the Boolean object which receives the message
to find the right solution.
I am not here to dictate what has to be decided.
This principle is found elsewhere too.
It's a fundamental principle of OOP.
It's the heuristics I mentioned in the beginning of this lesson:
don't ask, tell.
It means I don't want to express any conditionals;
I just want to give an order.
That's one of the important keys of OOP.
The other is about letting the receiver decide.
I give the receiver an order: it must encapsulate its knowledge
and make the right decisions.