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
We get 2 and not the set!
Why?
- The method
add:
returns its argument, not the receiver
A Verbose Solution
To get the collection back, we can use a temporary variable
yourself
add:
and yourself
are sent to the new Set
- the cascade (;) returns the returned value of
yourself
Another Idiom
is equivalent to
Using yourself
makes sure the code returns the new instance
What You Should Know
- Some simple methods are powerful
- Cascade
;
and yourself
often go together
/