[Newbies] Re: Two questions about Smalltalk language design
benschroeder at acm.org
Thu Dec 27 22:18:48 UTC 2012
On Thu, Dec 27, 2012 at 1:04 PM, Joseph J Alotta <joseph.alotta at gmail.com>wrote:
> 1. If a message does not return self, then you wouldn't be able to chain
> together or to cascade messages.
> for example:
> 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
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
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners