Hi, Chris, Monty, Tim,
did you see the implementation of Collection>>size?
size
"Answer how many elements the receiver contains."
| tally |
tally := 0.
self do: [:each | tally := tally + 1].
^ tally
Let's forget a minute about specific subclasses.
Does iterating over the whole collection is really the best idea for implementing isEmpty?
When we implement a message in Collection, we should think in term of Collection, not Interval, Set nor Array.
We're talking of several things here:
- universality of the default implementation:
we don't need to know about the size to tell if empty
not all collection have a known size
- optimization:
old implementation is fast for collections having an optimized size, catastrophic for others.
new implementation is reasonably fast, and full optimization can be restored with a few 1-liner.
- cleverness:
IMO this is not more clever than, and quite logically connected to implementation of size
I agree that having several implementations for the sake of optimization is not ideal.
That's what would have curbed my enthusiasm.
Though I doubt about any noticeable slowdown in anything else than micro-benchmarks.