Hello, everyone.
In this lecture we'll present class and method definitions
in Pharo.
The specific thing about Pharo is that
there's no dedicated syntax for defining methods and classes.
They're defined within tools.
The tools give the context to the creation of a class or method.
Here's the main code browser in Pharo:
Nautilus.
You can see in the bottom part
the definition of the class Point.
You can see that the class Point has the class Object as superclass
and two instance variables, x and y.
The superclass and the two instance variables, x and y.
And we see at the bottom the name of the package
in which this class is added.
In the slides we have code like this for creating a new subclass.
We can see that to create a class in Pharo
we send a dedicated message to the superclass
with the name of the subclass as parameter.
The message subclass: instanceVariableNames:
classVariableNames: package:
is a message sent to the class Object
with the name of the subclass as parameter,
the list of the instance variable names as parameter
and the name of the package as parameter.
As for the method definition,
we have, still in the bottom part...
the name of the method,
a comment
and the method's code.
And the method belongs to a class.
In the syntax to define the method
we have no information about the class to which the method should belong.
In fact, it's the selection of the class in the code browser
which tells us the class to which the method belongs.
In the slides we also need this information.
So we can't just copy the content of the definition of a method.
We have to add a prefix to the method name
to indicate the class to which this method belongs.
So we add...
Before the name of the method we add the name of the class
to which we're going to add the method we're defining.
So, in the code browser,
this class only appears as a selection
in the browser.
Here we define the method factorial of the class Integer.
A little reminder about the messages.
Here we're defining the method factorial.
We have all these binary messages...
equals, superior to, multiply by and minus...
Binary messages.
We have a recursive call to the method factorial.
Factorial, here, is a unary message
sent to an object of a number type.
And we have two keyword messages...
IfTrue:, twice, and error:.
And finally we have the caret,
used for returning a value
and leaving the method.
Regarding the return,
by default in Pharo
all methods return self,
the current object.
The code above
is exactly the same as the code below.
So the return self part at the end of the method is optional.
Class methods let you send messages to classes.
Classes are objects too,
so you can send messages to classes.
And to define a class method
you press the class button in Nautilus...
and define the class method.
In the slides we systematically add the word class,
to distinguish a method that'll be executed on a class
rather than on an instance of the class.
So here the method x: y: will be executed on the class Point,
not on an instance of the class Point.
What you should remember
from this lecture...
A class isn't defined by a particular syntax
but by sending a message to its superclass.
Classes are defined inside packages,
the names of which are passed as parameters of the message.
All methods are public.
There's no keyword to indicate that a method is public. They all are.
There's no notion of it being protected or private.
By default a method returns the receiver, which is self.
You can use the caret to return something other than self.
And class methods are methods like any other.
To add them, press the button class
before creating the method.
This concludes this first week of lectures.
In the coming weeks
we'll look in more detail at the syntax of Pharo,
blocks and a tool that is an object inspector.