[squeak-dev] Re: Collection>>sum implementation
Klaus D. Witzel
klaus.witzel at cobss.com
Tue Aug 12 14:29:06 UTC 2008
On Tue, 12 Aug 2008 15:10:57 +0200, Jason Johnson wrote:
> On Tue, Aug 12, 2008 at 1:32 PM, Klaus D. Witzel
> <klaus.witzel at cobss.com> wrote:
>> On Tue, 12 Aug 2008 09:28:22 +0200, Andreas Raab wrote:
>>
>>> Klaus D. Witzel wrote:
>>
>> [and I would object accessing inexisting elements for forcing an Error
>> :( ]
>
> It's pretty consistent for the default case to signal an error
This looks perhaps good but the problem here is that #anyOne causes an
indexing Error which n00bs and casual users will not immediately associate
with the cause (and it is raised by a "remote" method which knows nothing
about #sum). Users expect that Smalltalk's own collection protocols is
resistant against indexing errors as usual, if you know what I mean ;)
Therefore I'm in favor of Andreas' #ifNil:, not only because of its
elegance :)
And the essence of (x := y sum ifNil: [whatMyZeroIs]) is that the zero
element is defined by the user, not the implementor of #sum ... (and
#ifNil: can be omitted if you know your collections).
Regarding emptyCheck against #ifNil, your previous Q: during the loop, the
former is a full send (with even more own sends) but the latter is
inlined. For small collections one wouldn't notice a significant
difference. And if collections are soo huuge (Smalltalk's huge ;) then
people often implement/use the #fast* version of things.
> and
> provide an ifNone:/ifEmpty/etc. protocol for people who want other
> behavior, no? Of course unary messages participating in this protocol
> do look funny. :)
Sure, alternatives are appreciated.
More information about the Squeak-dev
mailing list
|