Yourself

Damien Cassou, Stéphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

A Puzzle and Candidate for Cascade

We add 2 to a set

Set new add: 2
> 2

We get 2 and not the set!

Why?

Set>>add: newObject
   "Include newObject as one of the receiver's elements, but only if not already present. Answer newObject."
   [...]
   ^ newObject
Set new add: 2
> 2

A Verbose Solution

To get the collection back, we can use a temporary variable

   | s |
   s := Set new.
   s add: 2.
   s

yourself

Object >> yourself
   ^ self
Set new
   add: 2;
   yourself
> aSet

Another Idiom

Set class >> with: anObject
   "Answer an instance of me containing anObject."
   | instance |
   instance := self new.
   instance add: anObject.
   ^ instance

is equivalent to

Set class >> with: anObject
   "Answer an instance of me containing anObject."
   ^ self new
      add: anObject;
      yourself

Using yourself makes sure the code returns the new instance

What You Should Know

/