help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] meaning of Collection>>#copyEmpty:


From: Stephen Compall
Subject: [Help-smalltalk] meaning of Collection>>#copyEmpty:
Date: Fri, 08 Dec 2006 23:35:57 -0600
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.8) Gecko/20061030 SeaMonkey/1.0.6

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]