﻿WEBVTT

00:00:00.560 --> 00:00:01.760 align:middle
Hello everyone.

00:00:01.920 --> 00:00:05.280 align:middle
In this course,
we'll learn about the method Yourself.

00:00:06.040 --> 00:00:10.520 align:middle
It is seemingly useless,
and yet it's very useful.

00:00:10.680 --> 00:00:12.280 align:middle
Let's start with an exercise.

00:00:12.440 --> 00:00:16.480 align:middle
For this expression, Set new add: 2,

00:00:18.320 --> 00:00:23.680 align:middle
we'd like to get a set
containing the value 2

00:00:23.840 --> 00:00:27.200 align:middle
so that it returns only the value 2.

00:00:27.360 --> 00:00:31.240 align:middle
Why do you get the value 2
rather than a set containing 2?

00:00:31.400 --> 00:00:34.880 align:middle
If you look at the method add:
implemented in the class Set,

00:00:35.040 --> 00:00:37.800 align:middle
you can see it returns its parameter.

00:00:42.320 --> 00:00:44.520 align:middle
In the expression Set new add: 2,

00:00:46.160 --> 00:00:48.880 align:middle
the expression Set
returns the class Set.

00:00:50.480 --> 00:00:54.960 align:middle
When you send new to the class Set,
it returns a new set.

00:00:55.960 --> 00:00:57.520 align:middle
Set new add: 2

00:00:57.680 --> 00:01:01.000 align:middle
returns the value returned by add: 2,
which is 2.

00:01:01.160 --> 00:01:02.720 align:middle
The set is lost.

00:01:02.880 --> 00:01:05.280 align:middle
To solve this problem,

00:01:05.440 --> 00:01:09.000 align:middle
you can describe the expressions
step by step.

00:01:09.840 --> 00:01:14.280 align:middle
You can create a temporary variable:
you declare the variable s.

00:01:15.320 --> 00:01:18.920 align:middle
Assign the new Set to s.

00:01:19.720 --> 00:01:21.800 align:middle
Add 2 to s.

00:01:22.920 --> 00:01:28.480 align:middle
s now contains
a set which contains the value 2.

00:01:28.640 --> 00:01:31.240 align:middle
It's what we wanted.

00:01:31.400 --> 00:01:33.920 align:middle
You can simplify these expressions

00:01:34.080 --> 00:01:36.440 align:middle
by using the method yourself.

00:01:36.600 --> 00:01:38.200 align:middle
If you take a closer look,

00:01:38.360 --> 00:01:42.960 align:middle
you'll see it contains only one thing:
return self.

00:01:43.120 --> 00:01:47.160 align:middle
Return self being optional,
this method could also be empty

00:01:47.320 --> 00:01:49.960 align:middle
and do nothing
except returning its receiver.

00:01:50.920 --> 00:01:55.280 align:middle
This method seemingly
only returns its receiver.

00:01:56.040 --> 00:01:59.680 align:middle
And yet, it's useful in such cases

00:02:00.800 --> 00:02:03.400 align:middle
thanks to the operator cascade.

00:02:03.560 --> 00:02:05.360 align:middle
After adding 2,

00:02:05.520 --> 00:02:08.960 align:middle
let's execute the method yourself

00:02:09.120 --> 00:02:11.040 align:middle
which will return the receiver

00:02:11.200 --> 00:02:14.360 align:middle
so that the full expression
can be the receiver,

00:02:14.520 --> 00:02:17.560 align:middle
which is the new Set.

00:02:18.480 --> 00:02:22.520 align:middle
If I take Set new,

00:02:22.680 --> 00:02:24.000 align:middle
which is the new Set,

00:02:24.760 --> 00:02:28.600 align:middle
and write add: 2 applied to Set new,

00:02:28.760 --> 00:02:30.320 align:middle
it returns 2.

00:02:30.480 --> 00:02:34.280 align:middle
But thanks to the operator cascade
followed by yourself,

00:02:34.440 --> 00:02:39.240 align:middle
the expression
as a whole returns the new Set.

00:02:40.360 --> 00:02:43.240 align:middle
Why does the cascade
systematically return

00:02:43.400 --> 00:02:45.760 align:middle
the value its last expression returns?

00:02:46.600 --> 00:02:48.800 align:middle
The cascade, in this case,

00:02:48.960 --> 00:02:52.840 align:middle
returns the value yourself returns.

00:02:53.000 --> 00:02:56.960 align:middle
We often use yourself and the cascade

00:02:57.120 --> 00:03:00.120 align:middle
in the methods of instantiation.

00:03:00.280 --> 00:03:04.840 align:middle
These are class methods

00:03:05.000 --> 00:03:07.280 align:middle
of messages to be sent to classes

00:03:07.440 --> 00:03:09.240 align:middle
which return new instances.

00:03:10.040 --> 00:03:12.360 align:middle
To create a new Set

00:03:12.520 --> 00:03:16.440 align:middle
which contains an object by default,

00:03:16.600 --> 00:03:21.680 align:middle
I can send the message with: anObject
to Set.

00:03:21.840 --> 00:03:25.520 align:middle
This code is executed
to create a new Set,

00:03:25.680 --> 00:03:27.920 align:middle
to put it in the variable instance,

00:03:28.680 --> 00:03:31.600 align:middle
to add to instance the new parameter,

00:03:31.760 --> 00:03:34.080 align:middle
and to return instance.

00:03:34.240 --> 00:03:38.080 align:middle
I can write this expression as a whole
by using yourself

00:03:38.240 --> 00:03:42.080 align:middle
in a shorter and more idiomatic way.

00:03:42.240 --> 00:03:45.920 align:middle
This code is typically found in Pharo.

00:03:46.080 --> 00:03:50.000 align:middle
It's important to master the semicolon,

00:03:50.160 --> 00:03:52.400 align:middle
the cascade, and yourself.

00:03:53.080 --> 00:03:56.920 align:middle
What you must remember
at the end of this course

00:03:57.080 --> 00:04:00.480 align:middle
is that some methods,
even though they're very simple,

00:04:00.640 --> 00:04:03.240 align:middle
can be very powerful and used a lot.

00:04:05.200 --> 00:04:09.440 align:middle
The cascade, the semicolon,
and the method yourself

00:04:09.600 --> 00:04:11.880 align:middle
are very often used together

00:04:12.040 --> 00:04:15.240 align:middle
to make sure a complete expression

00:04:15.400 --> 00:04:17.960 align:middle
returns the expected value.