Squeak Stream Questions

Boris Gaertner Boris.Gaertner at gmx.net
Sun Sep 19 16:07:37 UTC 2004


"stéphane ducasse" <ducasse at iam.unibe.ch> wrote:


> Hi all
>
> I'm checking whether my VW slides are working in Squeak and....huge
> disappointment.
> for example:
>
> |st|
> st := ReadWriteStream on: (OrderedCollection new: 5).
> st nextPut: 1.
> st contents.
>
> works perfectly in VW and breaks in Squeak.
Well, I often found it unconvenient that I cannot have
a WriteStream streaming over an OrderedCollection, but
I got accustumed to use an Array.

> What is the reason for such a difference?
The fact that  size has a different meaning for OrderedCollections and
Arrays.
(ar := Array new: 10) size  = 10
(oc := OrderedCollection new: 10) size = 0.
(str := String new: 10) size = 10.

The consequence of this difference is that
you can evaluate:
  ar at: 1 put: #foo
  str at: 1 put: $f
but for
oc at: 1 put: #foo
you get an index out ouf bounds error.

All this is also true for VW.
So, why can VW write into a Stream over an OrderedCollection
while Squeak cannot? The answer is not difficult, but it takes some
time to verify it from the code.
Here is what I found:

*The protocol of a WriteStream includes the capability to grow
a collection when it is full. In Squeak we simply create a larger collection
and copy the contents of the smaller collection into the larger one.
For an Array, the grown collection has a largersize, but for
an OrderedCollection, the size of the grown collection is that of
the smaller collection.
*In VW,  they do it smarter: When they grow an OrderedCollection,
they initialize all not immediately needed elements with nil.
That is the difference and of course we can do that too, if we
want to have it.

Attached you find a proposal to implement collection growing with
initialization of those collections that are not implemented as
variable classes. This change set is dangerous and requires
very careful testing.



> Why can I put an OrderedCollection in a stream?
See explanation above.

>
> |st|
> st := ReadWriteStream on: (Array new: 5).
> st nextPut: 1.
> st contents.
>
> works on both VW and Squeak
>

Greetings, Boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: growAndInitCollections.1.cs
Type: application/octet-stream
Size: 2032 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20040919/9fce6963/growAndInitCollections.1.obj


More information about the Squeak-dev mailing list