[squeak-dev] OrderedCollection and asOrderedCollection

H. Hirzel hannes.hirzel at gmail.com
Thu Jul 19 08:53:52 UTC 2012


On 7/18/12, Levente Uzonyi <leves at elte.hu> wrote:
> On Wed, 18 Jul 2012, 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.
>
> The name "as*" suggests me that the method won't make a copy if the
> receiver is already what I'm asking for. If I want to modify the returned
> collection, but leave the original one intact, then I'll send #copy.
>

+1

This is how I learned Smalltalk.  asNNN does a conversion in case it
is necessary otherwise if the object is already of type NNN I get back
the object as is.

--Hannes

>>
>> 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.
>
> This kind of safety has a huge cost in some cases, for example this
> is why so many people implement their own network streaming code. The less
> copies are made the higher throughput can be achieved.
>
>
> Levente
>
>>
>> - Bert -
>>
>>
>>
>>
>
>


More information about the Squeak-dev mailing list