Introduction to SmallTalk Object-Oriented Programming (Book notes)
Notes are heavily based on reading Programming Smalltalk - Object-Oriented from the Beginning: An introduction to the principles of programming by Johannes Brauer
This article follows on from a previous post: An introduction to object-oriented programming (Book Reading).
Messages in SmallTalk
The basic format for a message expression in Smalltalk is:
<object> signifies the object that is
&alt;message> (not the object
that is sending the message).
Messages that do not take an argument are called 'unary messages', while messages that contain exactly one argument are known as 'binary messages'.
Unary message examples:
2 negatedproduces the result -2.
Window newproduces a new window on the screen.
Binary message examples:
(b = 7) ifTrue: [do this]
(2 * 3)
Unary messages are always represented by a single word, while binary messages include arithmetic and comparison operators.
The third (and last) type of message in the SmallTalk language are 'keyword messages'. Keyword messages are characterised by a simple word with a colon attached to it.
Keyword message examples:
iftrue: [do something]
a max: b
Keyword messages can contain multiple keywords as a chain. They must also be followed by an argument that is written after the colon.
Order in which messages are sent in SmallTalk
The following list is extracted from the book
- Messages within parentheses are always evaluated first.
- Unary messages are evaluated before binary ones.
- Binary messages are evaluated before keyword messages.
- If an expression contains multiple messages of the same type, they are always processed from left to right.
Case by case distinctions is similar to if then else conditionals, except that the cases are determined using keyword messages.
(a = 0) ifTrue; [do this]
In this example, if the object is equal to '0' then return the result of the expression contained in the square brackets, otherwise return nil.
A block is a sequence of messages contained withing square brackets, where the messages are not immediately evaluated. Only the recieving object can determine whether the block of code may be evaluated. Blocks are only used in cases where sequences of messages are only to be executed under specific conditions.
Classes in Smalltalk
When using objects, it's important to consider which messages they can understand. For example, a Boolean object would not understand a message to square it's result.
Which message an object can understand depends on the class that it belongs to. The list of all methods in a class is called its 'method protocol'.
Different classes may accept the same message but execute that message differently. For example, both number and string classes accept comparison methods but execute them in different ways.
In SmallTalk, classes are also objects that have methods at their disposal, these are called 'class methods'.
Creating a new class using a constructor in SmalltalkSource: Object, Classes, Constructors, Smalltalk style
Creating an instance of the class
person := Person firstName: 'Ramon' lastName: 'Leon'.
Objects that you do not have to write yourself because they are built in are called literals. There are five types of literals: Numbers, characters, character strings, symbols and arrays.
There is lot of information about syntax in the book. I skimmed through that quickly instead of intensively studying it. I'll pick up the syntax as I need it when it comes to writing actual programs. For me the more important thing right now is understanding object oriented programming as a paradigm.