[Newbies] How to empty a collection?

Michael van der Gulik mikevdg at gmail.com
Tue Feb 19 20:32:51 UTC 2008


On Feb 20, 2008 8:09 AM, Randal L. Schwartz <merlyn at stonehenge.com> wrote:

> >>>>> "goran" == goran  <goran at krampe.se> writes:
> >> I'd just do:
> >>
> >> removeAll
> >> self notEmpty ifTrue: [self become: self species new].
> >>
> >> and let subclasses make it more efficient using implementation
> knowledge.
>
> goran> I would never use become: in "regular code" like this. :)
>
> First, how "regular" is it if the method hasn't been missed by anyone
> in 27 years. :)
>
> Second, what are you objecting to?  #become: is clearly a part of the
> image,
> and works fine.  This code also cleans out a grown collection to be the
> smallest it possiby can become, where delete-one-at-a-time solutions
> don't.
>


#become: and its friends are voodoo. Don't use them in your code unless you
have absolutely no other option.

It doesn't work fine. It breaks code that relies on hashes; specifically, a
Dictionary elsewhere in your image can start acting screwy if you do a
#become: variant that doesn't preserve an object's hash. See
#becomeForward>>copyHash:. You would then also be relying on a Squeakish
behaviour and your code would become less portable across Smalltalk
dialects.

They are also a fantastic way to introduce bugs of various sorts, and can
even cause an image to crash (as in, stack trace and exit to the OS). Say,
for example, that one of the arguments you pass is accidently nil, true or
false? Your image would continue working... for a while.

#become: and variants are also very slow and causes the equivalent of a full
GC on every invocation.

Gulik.


-- 
http://people.squeakfoundation.org/person/mikevdg
http://gulik.pbwiki.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20080220/fd83d838/attachment.htm


More information about the Beginners mailing list