binary selectors ambiguity and space

Duncan Mak duncanmak at gmail.com
Wed May 17 16:36:44 UTC 2006


On 5/17/06, Hans-Martin Mosner <hmm at heeg.de> wrote:
>
> The first is easy, but in my opinion does not add much value - "aNumber
> next" does not help me understand the code better than "aNumber + 1"
> does. And of course, it re-uses a selector which is already used in the
> stream hierarchy and means something completely different.

I agree the name might not be the best. I think in  Ruby, they have 'succ'.

> The second one is not possible with Smalltalk's semantics. Messages are
> sent to objects, not to variables. So whatever message you send to
> variable x, it can't cause x to point to another object (with the
> exception of #become: but that's a special case). Since numeric objects
> are immutable, you need to have the assignment x := x+1 either
> implicitly or explicitly somewhere.

I was thinking of using become: to implement something like this, yeah.

> I'd like to know concrete use cases where you would prefer an increment
> method over an explicit x := x+1. My gut feeling is that such cases
> probably could be handled even better by still other constructs (for
> example, collections or streams), but of course this can't be said
> generally for all cases.

I think you're probably right, but still I think having a particular
message send for 'increment' is not a bad idea in general. I think
something like this is most often used when implementing some sort of
counter.

It is true that

    names select: [:each | each startsWith: 'a'] size

is more elegant than something like:

    names do: [:each | each startsWith: 'a' ifTrue: [count := count + 1]

 but what if you want to count multiple conditions in a single iteration?

Duncan.


More information about the Squeak-dev mailing list