[BUG]Collection>>removeAll:

Göran Hultgren goran.hultgren at bluefish.se
Mon Aug 26 10:03:42 UTC 2002


Hi Richard and all!

Quoting "Richard A. O'Keefe" <ok at cs.otago.ac.nz>:
[BIG SNIP]
>     removeAll: aCollection
> 	aCollecton == self ifTrue: [^self removeAll: aCollection copy].
> 	^aCollection do: [:each | self remove: each]

Why not add a #removeAll and utilize that as I and also Martin Wirblat (see
below) propose? 

Martin Wirblat wrote:
> 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

Well, I think it was me, but whatever. :-)

> 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.

I also repeat that "x removeAll" is a much cleaner and more precise way than
writing explicitly:

x removeAll: x

When the developer in fact MEANS "x removeAll". And it gives us the ability to
implement it efficiently in subclasses.

(Of course sometimes the developer does not intend "x removeAll" but it just
happens that the collection sent in as a parameter is in fact x itself, so the
check for identity in removeAll: is necessary also.)

regards, Göran

PS. I am not sure I have read all postings in this matter so forgive me if I
have missed anything. DS

Göran Hultgren, goran.hultgren at bluefish.se
GSM: +46 70 3933950, http://www.bluefish.se
\"Department of Redundancy department.\" -- ThinkGeek



More information about the Squeak-dev mailing list