﻿WEBVTT

00:00:00.560 --> 00:00:04.240 align:middle
In this course, we'll study
Booleans and conditions in Pharo.

00:00:04.960 --> 00:00:09.160 align:middle
There are two Booleans objects:
true and false.

00:00:09.320 --> 00:00:12.480 align:middle
true is the only instance
of the class True.

00:00:12.640 --> 00:00:15.440 align:middle
false is the only instance
of the class False.

00:00:15.600 --> 00:00:20.640 align:middle
We'll send messages to these objects
to set Boolean operations

00:00:20.800 --> 00:00:24.160 align:middle
or alternative structures
such as ifTrue:IfFalse:, etc.

00:00:24.320 --> 00:00:26.280 align:middle
We'll study this.

00:00:26.920 --> 00:00:29.880 align:middle
In the first example of this slide,

00:00:30.040 --> 00:00:34.240 align:middle
you send the message &amp;,
a binary operator,

00:00:34.400 --> 00:00:35.720 align:middle
to the object false.

00:00:35.880 --> 00:00:38.520 align:middle
You pass it a parameter,
which is all this.

00:00:38.680 --> 00:00:42.320 align:middle
It's an expression between parentheses,
(1 error:'crazy').

00:00:42.480 --> 00:00:45.480 align:middle
This expression triggers an error.

00:00:45.640 --> 00:00:50.280 align:middle
You can see the evaluation of this
whole expression triggers an error.

00:00:50.440 --> 00:00:53.600 align:middle
Why? Because the operand
has also been evaluated

00:00:53.760 --> 00:00:56.280 align:middle
in this Boolean expression.

00:00:57.560 --> 00:01:01.000 align:middle
The operand has been evaluated,
hence the mistake.

00:01:01.160 --> 00:01:05.640 align:middle
If you want to get the lazy version
of the Boolean operators,

00:01:05.800 --> 00:01:10.960 align:middle
you must use the method and:
of the class Boolean.

00:01:11.120 --> 00:01:13.440 align:middle
This method takes a block
as a parameter.

00:01:13.600 --> 00:01:18.000 align:middle
Blocks start with [ and end with ].

00:01:18.160 --> 00:01:21.320 align:middle
They contain expressions,
but the definition of a block

00:01:21.480 --> 00:01:24.800 align:middle
doesn't trigger
the execution of its expressions.

00:01:24.960 --> 00:01:28.640 align:middle
For now, these expressions
are not evaluated.

00:01:28.800 --> 00:01:31.600 align:middle
It is only if the first operand
is true or false

00:01:32.320 --> 00:01:35.600 align:middle
that the block passed as a parameter
is evaluated.

00:01:35.760 --> 00:01:39.880 align:middle
In the second example,
you have false and: a block.

00:01:40.040 --> 00:01:45.440 align:middle
It returns false without evaluating
the block passed as a parameter.

00:01:45.960 --> 00:01:49.360 align:middle
It is a typical lazy operator
but built using blocks.

00:01:51.400 --> 00:01:55.160 align:middle
In Pharo, conditionals
(if, false, while, etc.)

00:01:55.320 --> 00:01:59.160 align:middle
are always messages
sent to Booleans or blocks.

00:02:00.080 --> 00:02:01.800 align:middle
Let's take another example:

00:02:01.960 --> 00:02:06.960 align:middle
the message ifTrue:ifFalse:, the typical
alternative of any programming language.

00:02:07.120 --> 00:02:09.280 align:middle
But in Pharo, it's actually a message:

00:02:09.440 --> 00:02:13.440 align:middle
I can send the message ifTrue:ifFalse
to a Boolean object.

00:02:13.600 --> 00:02:16.480 align:middle
This expression returns a Boolean.

00:02:17.840 --> 00:02:22.760 align:middle
I pass two blocks as parameters
of this message:

00:02:22.920 --> 00:02:26.560 align:middle
one block is evaluated
if the receiver Boolean is true,

00:02:26.720 --> 00:02:29.480 align:middle
the other if the receiver is false.

00:02:30.560 --> 00:02:33.200 align:middle
Careful: the message ifTrue:ifFalse:

00:02:33.360 --> 00:02:37.000 align:middle
is heavily used by Pharo:
it's at the heart of the system.

00:02:37.160 --> 00:02:42.320 align:middle
It's so important
that it's been heavily optimized

00:02:42.480 --> 00:02:46.480 align:middle
to be efficient without slowing down
the entire system.

00:02:47.800 --> 00:02:51.160 align:middle
Let's talk again
about Boolean implementation in Pharo:

00:02:51.320 --> 00:02:53.360 align:middle
it's extremely elegant.

00:02:53.520 --> 00:02:59.000 align:middle
the objects true and false are both
instances of the classes True and False

00:02:59.160 --> 00:03:01.400 align:middle
which inherit the class Boolean.

00:03:01.560 --> 00:03:04.760 align:middle
Each of these classes
define a set of methods

00:03:04.920 --> 00:03:07.640 align:middle
to return the expected behavior.

00:03:07.800 --> 00:03:13.080 align:middle
We'll give you more information
about this elegant implementation

00:03:13.240 --> 00:03:15.200 align:middle
in a further lesson.

00:03:16.640 --> 00:03:19.560 align:middle
Back to the message ifTrue:ifFalse:.

00:03:19.720 --> 00:03:23.200 align:middle
There are different versions
of these messages.

00:03:23.360 --> 00:03:28.040 align:middle
There's a version with only ifTrue:
with a block as a parameter

00:03:28.960 --> 00:03:31.760 align:middle
to return only the first part
of the alternative.

00:03:31.920 --> 00:03:34.960 align:middle
The message ifTrue:ifFalse
takes two blocks

00:03:35.120 --> 00:03:39.480 align:middle
to return an alternative
when the Boolean is true or false.

00:03:40.520 --> 00:03:43.720 align:middle
There are several examples here.

00:03:43.880 --> 00:03:46.480 align:middle
This only sends the message ifTrue:

00:03:46.640 --> 00:03:49.960 align:middle
with only one block to execute
if the expression is true.

00:03:50.120 --> 00:03:53.080 align:middle
In this example,
there's one block to execute:

00:03:53.240 --> 00:03:55.920 align:middle
this one if the receiver Boolean
is true,

00:03:56.080 --> 00:03:58.320 align:middle
that one if it is false.

00:04:00.960 --> 00:04:02.960 align:middle
There are two different messages.

00:04:03.120 --> 00:04:05.120 align:middle
It's the same with ifFalse:

00:04:05.280 --> 00:04:08.760 align:middle
when it comes to the classes
Boolean, True, and False.

00:04:10.400 --> 00:04:15.560 align:middle
There are other types of messages
available to express conditions.

00:04:15.720 --> 00:04:18.360 align:middle
For instance,
there's ifEmpty: ifNotempty:

00:04:18.520 --> 00:04:20.600 align:middle
which I can send to a collection.

00:04:20.760 --> 00:04:23.920 align:middle
With ifEmpty:, I pass a block.

00:04:24.920 --> 00:04:29.200 align:middle
The block is evaluated only if the
receiver collection is or isn't empty.

00:04:29.960 --> 00:04:34.480 align:middle
ifNotEmpty: is the opposite condition.

00:04:34.640 --> 00:04:38.240 align:middle
If the collection isn't empty,
then you execute the block.

00:04:38.400 --> 00:04:40.920 align:middle
But there's an extra problem:

00:04:41.080 --> 00:04:45.080 align:middle
this block takes an argument
as a parameter

00:04:45.240 --> 00:04:47.680 align:middle
which is the non-empty collection.

00:04:47.840 --> 00:04:51.880 align:middle
The collection might be the result
of a complex computation.

00:04:52.040 --> 00:04:54.520 align:middle
You can query once more
for the collection.

00:04:54.680 --> 00:04:58.600 align:middle
If the collection isn't empty,
it is passed as the block's parameter.

00:04:58.760 --> 00:05:01.320 align:middle
It can be used
in the expressions of the block.

00:05:02.160 --> 00:05:03.920 align:middle
In this session,

00:05:04.080 --> 00:05:06.800 align:middle
we saw Booleans
are normal objects in Pharo

00:05:06.960 --> 00:05:10.800 align:middle
and instances of the classes True
and False which inherit Booleans.

00:05:10.960 --> 00:05:12.560 align:middle
You can send them messages.

00:05:12.720 --> 00:05:18.240 align:middle
These messages define
all the constructions of alternatives

00:05:18.400 --> 00:05:21.520 align:middle
or of possible conditions in Pharo.

00:05:21.680 --> 00:05:26.440 align:middle
In other languages, it would rather be
structures in the compiler.