[BUG]Collection>>removeAll:

Martin Wirblat mw.projanynet at SoftHome.net
Sat Aug 24 13:22:56 UTC 2002


a removeAll: b 
is supposed to remove all elements in a which are occurring in b.

The message selector suggests this, the programmer thinks of it as this
and the implementor can make it like this.

=> this behaviour is independent of the relation of a and b
   ( a ~= b, a = b, a == b ) It is a behaviour of the elements and not 
   of the identities of the collections.

=> There is absolutely no need to raise an exception for a == b, only 
the 
   current bad implentation may make us think that there is. An 
   exclusion of a == b would be an unnecessary severe restriction.

I think Richard A. O'Keefe is right with his fix, of course as I 
pointed out in an earlier post, without breaking the external protocol 
with a different answer.

In one subclass of Collection there is already #removeAll implemented, 
so my suggestion would be:

Collection >>
removeAll: aCollection
    self == aCollection
        ifTrue: [ self removeAll ]
        ifFalse: [ aCollection do: [ :each | self remove: each ]].
    ^ aCollection

The default version which argueable could be 'self 
subclassResponsibility' 

Collection >>
removeAll
    self copy do: [:each | self remove: each]

And as an example for the overriding essentielly as 
Richard A. O'Keefe suggested

OrderedCollection >>
removeAll
    self setCollection: ( Array new: array size )

There will be no speed difference and no memory blow up, because all
classes which implement #add: and #remove: have to be able to grow,
which means, they have to have named instance vars, and there should
be always an efficient shortcut to reset them.

regards, Martin





More information about the Squeak-dev mailing list