[Cuis-dev] learning about streams

Gerald Klix cuis.01 at klix.ch
Sun Sep 22 10:28:40 PDT 2024


On Sun, 22 Sep 2024 10:59:12 -0500
Mark Volkmann via Cuis-dev <cuis-dev at lists.cuis.st> wrote:

> Consider this simple example:
> 
> coll := OrderedCollection newFrom: #('apple' 'banana' 'cherry').
> stream := ReadWriteStream on: coll.
> stream next print.
> 
> I expected this to print apple in the Transcript (and it does if I use a
> ReadStream), but it prints nil. The reason is that the on: method of
> WriteStream, which is the superclass of ReadWriteStream, does this to set
> readLimit:
> 
> on: aCollection
>     super on: aCollection thatCanBeModified.
>     readLimit := 0.
>     writeLimit := aCollection size
> 
> The superclass PositionableStream on: method sets readLimit to the size of
> the collection (3 in this case), but here readLimit is changed to zero. Why
> does it do that? This seems to prevent a ReadWriteStream from reading.
> 
> I got some clues from the class command for ReadWriteStream which says the
> following:
> 
> Instances are created on: aCollection, which is considered to be empty or
> filled with nil. Any contents will be ovewriten, and are not available for
> reading.
> Instances can also be create with: aCollection. In this case, new contents
> will be added after this initial stuff. Reading them is only possible if
> position: is set appropriately (for instance aReadWriteStream position: 1).
> 
> Side note: There are two typos in the class comment. "overwriten" should be
> "overwritten" and "be create" should be "be created".
> 
> When I add "stream position: 1" I get "Error: Attempt to set the position
> of a PositionableStream out of bounds".
> 
> -- 
> R. Mark Volkmann
> Object Computing, Inc.

Hi Mark,

I aggree with you. In your case you should use ReadWriteStream class>>#with:
and than send #reset to the stream just created.

You will find the documentation of the constructor methods implemented
at the class side in the methods of the same name at the instance side, e.g.
WriteStream>>#with: and WriteStream>>#on:.

Admittedly the documented behaviour is a bit surprising, to say the least.
I definitly was surprised!


HTH and Best Regards,

Gerald


More information about the Cuis-dev mailing list