[BUG]Stream class>on:from:to: & reset

Dan Ingalls Dan.Ingalls at disney.com
Thu Sep 2 22:33:25 UTC 1999


>If one makes a ReadStream or WriteStream using #on:from:to:, you get to
>stream on the contents of the collection starting at the from'th element
>and ending at the to'th element.
>
>Unfortunately if you the reset the stream, it will start at the first
>element, no matter what the from index was supposed to be!
>
>The bad news is that fixing this _could_ be a bit tricky, since we would
>need to add an instvar to each class, and this would mess up any prims
>relying on instvar numbering; I can't see any right now, but that doesn't
>mean there aren't any! If we're safe to add and instvar, the fix is pretty
>trivial. 

You are right, this would foul up the nextPut primitive for WriteStream, so you would have to add the new field separately after those defined in ReadStream and WriteStream rather than doing it in one place in PositionableStream.  Otherwise, it should be straightforward, but a bit of a shame to add something that's hardly ever used (though, I admit, frustrating when it's not there).

Another approach would be to use a mappedCollection (slower) when you really want to reset a partial stream.

Thus, whereas...
 | s | s := ReadStream on: 'abcdefghi' from: 3 to: 5.
^ String streamContents: [:ss | 3 timesRepeat: [ss nextPutAll: (s next: 3). s reset]]
....produces 'cdeabcabc', ...

 | s | s := ReadStream on: (MappedCollection collection: 'abcdefghi' map: (3 to: 5)).
^ String streamContents: [:ss | 3 timesRepeat: [ss nextPutAll: (s next: 3). s reset]]
....produces 'cdecdecde'.

Obviously a bit of syntactic sugar would help this example as, say,
     s := ReadStream on: ('abcdefghi' mapFrom: 3 to: 5)
....or even...
     s := ReadStream on: 'abcdefghi' resettableFrom: 3 to: 5

	- Dan





More information about the Squeak-dev mailing list