[Newbies] Re: Two questions about Smalltalk language design

Levente Uzonyi leves at elte.hu
Sun Dec 30 15:53:39 UTC 2012


On Thu, 27 Dec 2012, Benjamin Schroeder wrote:

> 
> 
> 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 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
> 
> 
>


More information about the Beginners mailing list