WEBVTT

00:00:00.400 --> 00:00:01.640 align:middle
Hello.

00:00:02.240 --> 00:00:06.560 align:middle
Today we will review
the implementation of ifTrue:ifFalse:

00:00:06.720 --> 00:00:09.920 align:middle
If you've been attentive
and I'm sure you have,

00:00:10.080 --> 00:00:14.320 align:middle
you surely noticed
that when I suggested examples for Not,

00:00:14.480 --> 00:00:17.920 align:middle
I said you could implement
Not, Or and ifTrue:ifFalse:

00:00:18.080 --> 00:00:21.320 align:middle
But in the solution,
I never mentioned ifTrue:ifFalse:

00:00:21.480 --> 00:00:23.720 align:middle
We'll solve this problem now.

00:00:24.440 --> 00:00:28.760 align:middle
In effect, in Pharo,
conditions are messages.

00:00:28.920 --> 00:00:30.480 align:middle
Here's an example.

00:00:30.640 --> 00:00:34.480 align:middle
We have a weather class:
"Weather isRaining".

00:00:34.640 --> 00:00:36.600 align:middle
This means that if it rains...

00:00:37.240 --> 00:00:40.880 align:middle
If it's true, I'll take my umbrella.

00:00:41.040 --> 00:00:43.400 align:middle
If it's false, I'll take my sunglasses.

00:00:43.760 --> 00:00:45.840 align:middle
With ifTrue:ifFalse:

00:00:46.000 --> 00:00:50.960 align:middle
You see the two colons?
This means they're keyword messages.

00:00:51.120 --> 00:00:54.440 align:middle
The message ifTrue:ifFalse:
is a keyword message

00:00:54.600 --> 00:00:57.320 align:middle
and it's sent to a boolean instance.

00:00:58.160 --> 00:01:02.720 align:middle
In reality, it is heavily optimized
by the compiler and not really sent,

00:01:02.880 --> 00:01:05.640 align:middle
but conceptually speaking it is.

00:01:05.800 --> 00:01:10.960 align:middle
So if you want to redefine
the method ifTrue:

00:01:11.120 --> 00:01:12.640 align:middle
ifFalse:

00:01:12.800 --> 00:01:14.760 align:middle
You can do this in Pharo.

00:01:14.920 --> 00:01:17.240 align:middle
I'll explain the technique now.

00:01:17.400 --> 00:01:20.880 align:middle
The ifTrue:ifFalse:
method is so important

00:01:21.040 --> 00:01:23.520 align:middle
and we need it to go so fast,

00:01:23.680 --> 00:01:26.800 align:middle
that the function
is not actually invoked.

00:01:27.160 --> 00:01:29.400 align:middle
The compiler makes jumps inline instead.

00:01:29.560 --> 00:01:33.240 align:middle
But for the purpose of this class,
we'll see how it functions.

00:01:33.400 --> 00:01:35.960 align:middle
I want you
to propose an implementation.

00:01:36.120 --> 00:01:37.800 align:middle
You know about blocks.

00:01:37.960 --> 00:01:40.920 align:middle
You know what we did with
True and False,

00:01:41.120 --> 00:01:42.720 align:middle
and with Or and Not.

00:01:42.880 --> 00:01:45.440 align:middle
You can do it with ifTrue:ifFalse:

00:01:45.600 --> 00:01:49.960 align:middle
In sum, if my receiver is false,
I return 5.

00:01:50.120 --> 00:01:52.880 align:middle
And if my receiver is true,
I return 3.

00:01:53.600 --> 00:01:56.920 align:middle
Once again, we have objects,
messages and blocks.

00:01:57.080 --> 00:02:00.000 align:middle
It's a bit repetitive,
but that's the way it is.

00:02:00.600 --> 00:02:03.480 align:middle
You remember that brackets...

00:02:05.960 --> 00:02:08.080 align:middle
freeze the execution

00:02:08.240 --> 00:02:10.800 align:middle
of the expression they contain,

00:02:10.960 --> 00:02:15.360 align:middle
and that "value"
kicks the execution of a frozen code.

00:02:15.520 --> 00:02:18.680 align:middle
With this, and Not and Or,

00:02:18.840 --> 00:02:21.480 align:middle
you should be able
to implement your conditions.

00:02:21.640 --> 00:02:25.280 align:middle
Likewise, you should
be able to implement ifTrue:ifFalse:

00:02:25.440 --> 00:02:27.960 align:middle
I'll give you a few seconds to think.

00:02:29.000 --> 00:02:30.720 align:middle
How is it implemented?

00:02:30.880 --> 00:02:34.360 align:middle
In exactly the same manner
as Or and Not.

00:02:34.520 --> 00:02:37.280 align:middle
In the True class,
I have a method called

00:02:37.440 --> 00:02:39.920 align:middle
ifTrue: ifFalse: and two arguments.

00:02:40.080 --> 00:02:44.280 align:middle
One block for True
and one block for False.

00:02:44.440 --> 00:02:45.520 align:middle
What do I do?

00:02:45.680 --> 00:02:48.320 align:middle
When I'm in the True block,
I execute True.

00:02:49.600 --> 00:02:52.160 align:middle
And when I'm in the False class,

00:02:52.320 --> 00:02:56.240 align:middle
I have the same method
with the same two arguments,

00:02:56.400 --> 00:03:00.160 align:middle
but since I'm in the False class,
I execute the False block.

00:03:00.520 --> 00:03:03.080 align:middle
I have the booleans' implementation.

00:03:03.240 --> 00:03:04.440 align:middle
How does it work?

00:03:05.400 --> 00:03:06.520 align:middle
Like this.

00:03:06.680 --> 00:03:10.160 align:middle
When I send a message to my boolean,

00:03:10.320 --> 00:03:12.560 align:middle
which is in fact True,

00:03:12.720 --> 00:03:16.480 align:middle
I send ifTrue: do something True
or do something False.

00:03:16.640 --> 00:03:20.520 align:middle
Then I look up ifTrue:ifFalse:
in the True class,

00:03:20.680 --> 00:03:21.920 align:middle
It's right here.

00:03:22.080 --> 00:03:25.440 align:middle
It says to execute the True block,
so I do.

00:03:25.600 --> 00:03:29.600 align:middle
Now I send the message
"B is a boolean: False."

00:03:29.760 --> 00:03:33.800 align:middle
I send the message and look here.
I find ifTrue:ifFalse:

00:03:33.960 --> 00:03:38.760 align:middle
It says to execute the False block,
and that's what I do.

00:03:39.200 --> 00:03:43.480 align:middle
You cannot test this by putting
a breakpoint in ifTrue:ifFalse:

00:03:43.640 --> 00:03:46.400 align:middle
It's optimized
and the system won't accept it.

00:03:46.560 --> 00:03:50.840 align:middle
But what I do suggest you try,
is implement in the same manner:

00:03:51.000 --> 00:03:52.800 align:middle
ifFalse: or ifTrue:

00:03:52.960 --> 00:03:55.080 align:middle
ifTrue: a Block 1,

00:03:56.160 --> 00:04:00.680 align:middle
and ifFalse: A Block 2.
Then implement them.

00:04:00.840 --> 00:04:03.280 align:middle
Use a breakpoint and experiment.

00:04:03.880 --> 00:04:07.920 align:middle
So what have we shown
with this example?

00:04:08.080 --> 00:04:11.640 align:middle
We saw that sending a message
selects the right method.

00:04:11.800 --> 00:04:16.320 align:middle
Here we decided
to let the receiver decide its behavior.

00:04:16.480 --> 00:04:21.400 align:middle
And we saw that brackets
freeze computation,

00:04:21.560 --> 00:04:24.840 align:middle
and that "value"
forces the execution of a frozen code.

