On 8/26/07, Bert Freudenberg bert@freudenbergs.de wrote:
On Aug 26, 2007, at 11:01 , Fabio Filasieno wrote:
collection := OrderedCollection new. collection add:1.
evaluates to one. Why ? Why not to collection ?
You do not always add a simple value but the result of an expression. Having the #add: return the argument sometimes lets you avoid introducing a temporary variable. It is more useful this way, and for the cases where you actually want the collection you can use a cascade.
A similar point can be made that setters should return the argument instead of the receiver, although usually in Squeak they do not (Tweak revises that policy).
- Bert -
+1, exactly. By returning the argument you have more options then if you return self. You could do something like:
addToJohn: aSalary andFred: anotherSalary self totalSalary: (john addToSalary: aSalary) + (fred addToSalary: anotherSalary)
If all those setters return self then this takes several more statements. And there is no need to return self, as it is already accessible via cascade (as Bert pointed out).