binary selectors ambiguity and space
Dan at SqueakLand.org
Wed May 17 19:28:15 UTC 2006
>Duncan Mak wrote:
>>I have always wondered why the standard image doesn't come with a next method and an increment method for Integers, where next would answer x + 1 and increment would set x to be x + 1.
and Hans-Martin Mosner <hmm at heeg.de> replied...
>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.
>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'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.
We have several times contemplated a Smalltalk with the completely complementary point of view about integers - namely that the system is built around accumulator objects - mutable holders of raw integer values. If you follow this through, it has significant benefits:
1. You get a homogeneous address space - no more tag bits to check for SmallInts.
2. The format and range of integers is the same as C and native code.
3. Patterns like "i increment" can be wicked fast because the receiver knows the type,
can use a native increment instruction, and can use the native overflow
test to check bounds.
4. The extension to Floats has similar benefits, plus
iterative code need not create tons of little objects for intermediate values
(ie (0.0 to: 10000 by: 0.01) do:  would not create a million Floats
Of course literals would use read-only subclasses of these objects.
I post this just because it's fun to think about and share such alternate approaches. In spite of these attractive characteristics, I like the functional composition of immutable objects as the basic model to teach and work with, and I think the average newbie is a bit more ready for integers than accumulators.
More information about the Squeak-dev