Background
Something that catches every Smalltalk newbie is that add:
does not return "self" but the object being added.
For example, with this code:
myCollection := OrderedCollection new
add: 'Peter';
add: 'John';
add: 'Paul'.
myCollection
will containt the String "Paul", and not the collection itself.
This is because add:
returns the object being added, and the whole cascade expression evaluates to the last message being sent.
Instead, it should be written with yourself
at the end:
myCollection := OrderedCollection new
add: 'Peter';
add: 'John';
add: 'Paul';
yourself.
Questions
- Why is this so?
- What was this designed this way?
- What are the benefits of
add:
behaving this way?