1. If a message does not return self, then you wouldn't be able to chain messages together or to cascade messages.
for example:
|s|
s := Sphere new.
s rotateLeft; rotateRight; spin 60.
would have to be:
s rotateLeft. s rotateRight. s spin 60.
2. If you add an object to a collection, most times you are still concerned with the object, not the collection. I know what you mean that a lot of times you build an object and when it is complete you store it in a collection and move onto something else. But there is no reason adding has to be the final step.
For example, I pour coffee into my cup, (Cup new) add: coffee, then I stir the coffee, (coffee stir) .
- Why do ST methods return "self" if nothing is explicitly returned?
(he would have expected something like "Unit" in Scala, or "Void" -
- In Collections, why does "add:" return the object being added, and
not "self"?
On Thu, Dec 27, 2012 at 1:04 PM, Joseph J Alotta joseph.alotta@gmail.comwrote:
- If a message does not return self, then you wouldn't be able to chain
messages together or to cascade messages.
for example:
|s|
s := Sphere new.
s rotateLeft; rotateRight; spin 60.
would have to be:
s rotateLeft. s rotateRight. s spin 60.
Cascading using semicolons would still work if methods didn't answer self, but would be less useful in some circumstances.
The cascade rule is that subsequent messages get sent to the same receiver as the most recent one, so in
s rotateLeft; rotateRight; spin: 60
all of the messages get sent to s - no matter what they return.
But here's where it's useful for simple modifier messages like these to answer self: say we had
s := Sphere new rotateLeft; rotateRight; spin: 60.
Here the last three messages get sent to the result of (Sphere new). The result of the the entire cascade is the result of the last message sent, spin:. Since spin: answers self, we can do the Sphere creation and setup all in one cascade. Otherwise we'd have to split it:
"If spin: didn't answer self" s := Sphere new. s rotateLeft; rotateRight; spin: 60.
Ben
On Thu, 27 Dec 2012, Benjamin Schroeder wrote:
On Thu, Dec 27, 2012 at 1:04 PM, Joseph J Alotta joseph.alotta@gmail.com wrote: 1. If a message does not return self, then you wouldn't be able to chain messages together or to cascade messages.
for example: |s| s := Sphere new. s rotateLeft; rotateRight; spin 60. would have to be: s rotateLeft. s rotateRight. s spin 60.
Cascading using semicolons would still work if methods didn't answer self, but would be less useful in some circumstances.
The cascade rule is that subsequent messages get sent to the same receiver as the most recent one, so in
s rotateLeft; rotateRight; spin: 60
all of the messages get sent to s - no matter what they return.
But here's where it's useful for simple modifier messages like these to answer self: say we had
s := Sphere new rotateLeft; rotateRight; spin: 60.
Here the last three messages get sent to the result of (Sphere new). The result of the the entire cascade is the result of the last message sent, spin:. Since spin: answers self, we can do the Sphere creation and setup all in one cascade. Otherwise we'd have to split it:
"If spin: didn't answer self" s := Sphere new. s rotateLeft; rotateRight; spin: 60.
There are two other ways to write this code if you don't want to rely on the return value of #spin: :
1:
(s := Sphere new) rotateLeft; rotateRight; spin: 60
2: s := Sphere new rotateLeft; rotateRight; spin: 60; yourself
Levente
Ben
beginners@lists.squeakfoundation.org