meaning of Collection>>#copyEmpty:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

meaning of Collection>>#copyEmpty:

S11001001
copyEmpty: newSize

     "Answer an empty copy of the receiver whose size is newSize"

     ^self species new: newSize

Clearly, this has a different meaning when the argument to new: is
treated as a capacity.  Furthermore, is #(nil nil nil nil nil)
considered an empty Array?

I recently discovered that, due to this discrepancy, OrderedCollections
cannot be used as contents of WriteStreams.  This isn't a major problem,
because WriteStreams are after all designed to do the extra work that
OrderedCollections do for you.  And Squeak, at least, shares this "feature".

I suppose my real question is whether this method can be rewritten in a
way such that it would work sensibly for OrderedCollections without
expanding the Collection protocol or doing explicit isKindOf: tests? --
not that I would recommend replacing this perfectly sensible
implementation for such a silly purpose at all:

growCollection

    "Private - Double the size of the collection or, if its size < 8,

     grow it to 8 places"

    | size |

    size := collection size.

 

    "Guess which collection is which :-)"

    collection := ((collection copyEmpty: (size * 2 max: 8))

         replaceFrom: 1 to: collection size with: collection startingAt:
1;
         yourself).

--
Stephen Compall
http://scompall.nocandysw.com/blog
##smalltalk,#gnu-smalltalk on Freenode IRC


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: meaning of Collection>>#copyEmpty:

Paolo Bonzini

> Clearly, this has a different meaning when the argument to new: is
> treated as a capacity.  Furthermore, is #(nil nil nil nil nil)
> considered an empty Array?

As empty as possible, given that Array is not extensible.

> I recently discovered that, due to this discrepancy, OrderedCollections
> cannot be used as contents of WriteStreams.  This isn't a major problem,
> because WriteStreams are after all designed to do the extra work that
> OrderedCollections do for you.  And Squeak, at least, shares this
> "feature".
>
> I suppose my real question is whether this method can be rewritten in a
> way such that it would work sensibly for OrderedCollections without
> expanding the Collection protocol or doing explicit isKindOf: tests? --
> not that I would recommend replacing this perfectly sensible
> implementation for such a silly purpose at all:

I don't follow you really, but if you propose a patch to make
WriteStreams work on extensible collections, I'll be happy to review it.

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk