binary selectors ambiguity and space
ncellier at ifrance.com
Wed May 17 17:10:41 UTC 2006
Le Mercredi 17 Mai 2006 18:36, Duncan Mak a écrit :
> On 5/17/06, Hans-Martin Mosner <hmm at heeg.de> wrote:
> > 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.
become: Cannot work on Integer, and beware of side effects, value will change
also for every object pointing on it...
You can simulate this count++ like with an indirection (a special ValueHolder
subclass Counter) send the #increment message or whatever to the Counter, and
then retrieve its value, but i don't know if this is interesting, apart
sharing a common value between multiple objects
counter := Counter new.
names do: [:each | each startsWith: 'a' ifTrue: [count increment]].
> 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]
names count: [:each | each startsWith: 'a']
is even more efficient (does not create a copy of the collection)
> but what if you want to count multiple conditions in a single iteration?
More information about the Squeak-dev