[Newbies] Re: Two questions about Smalltalk language design

Benjamin Schroeder 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
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20121227/56d967c4/attachment.htm


More information about the Beginners mailing list