[squeak-dev] OrderedCollection and asOrderedCollection

David T. Lewis lewis at mail.msen.com
Wed Jul 18 23:46:34 UTC 2012


On Wed, Jul 18, 2012 at 02:33:23PM -0700, Bert Freudenberg wrote:
> On 18.07.2012, at 14:08, Frank Shearar wrote:
> 
> > Is there any particular reason why we don't have OrderedCollection >>
> > #asOrderedCollection simply return self?
> > 
> 
> > Right now (OrderedCollection with: 1) asOrderedCollection calls
> > Collection >> #asOrderedCollection, which calls self as:
> > OrderedCollection, which copies the collection.
> > 
> > This would also make OrderedCollection work a bit more like Array -
> > Array >> #asArray simply returns self.
> > 
> > frank
> 
> 
> Here's my guess:
> 
> The typical use cases for asOrderedCollection and asArray are different. If you send asOrderedCollection you probably want to add more objects to it. If you send asArray you normally won't modify the result.
> 
> So in a way, you could see asOrderedCollection as the normal "safe" case which ensures the original is unmodified, and the asArray behavior as an optimization.
> 
> - Bert -
> 

It also ensures that subclasses respond reasonably to #asOrderedCollection.
For example, if you send #asOrderedCollection to a WeakOrderedCollection
or a SortedCollection, it is probably because you actually want to obtain
an OrderedCollection:

(WeakOrderedCollection withAll: #(1 2 3 4)) asOrderedCollection
     ==> an OrderedCollection(1 2 3 4)

(SortedCollection withAll: #(4 2 3 1)) asOrderedCollection
     ==> an OrderedCollection(1 2 3 4)

Dave
 


More information about the Squeak-dev mailing list