Hi Andrew, My first hunch is that you don't want to change PositionableStream. Because it works on collections and files (because of inheritance). But not on other kind of streams that are not part of the hierarchy, such as sockets. And we are cleaning that part. Instead, some time ago Sven produced a really nice hierarchy of stream decorators. Check the package: Zinc-Character-Encoding-Core There you have ZnBufferedReadStream That implements buffering in an orthogonal way. Maybe it makes more sense to put such method there? Guille On Thu, Nov 2, 2017 at 9:59 AM, Prof. Andrew P. Black <black@cs.pdx.edu> wrote:
I sometimes find, when working with a stream, that I need to "peek aheadâ a few items, without disturbing the state of the stream. The message stream peek gives me the next item, but doesnât generalize to multiple items, So I end up writing code like this:
findSomething | savedPosition result | savedPosition := stream position. stream position: start. âcode involving various manipulations of stream, including stream next" result := stream position + 1. stream position: savedPosition. ^ result
This code involves at least two temps, is not very easy to read, and risks not resetting the stream properly if I do a return.
It seems to me that a better solution would be what Kent Beck calls an âexecute aroundâ method:
findSomething ^ stream unchangedDuring: [ :s | âcode involving various manipulations of stream, including stream next" stream position + 1 ]
Iâm not sure that I like the name #unchangedDuring:; perhaps you can think of a better one?
The implementation of this would be
PositionableStream >> #unchangedDuring: aBlock "execute aBlock with self as argument. Ensure that when this method returns, I still have my current state." | savedPosition | savedPosition := self position. ^ [ aBlock value: self ] ensure: [ self position: savedPosition ]
or perhaps
PositionableStream >> #unchangedDuring: aBlock "execute aBlock with self as argument. Ensure that when this method returns, I still have my current state."
^ [ aBlock value: self copy ]
which might be better in the case that the stream is a file stream and aBlock causes the buffer to be refreshed from the disk.
- Do other Smalltalks have a method like this? - Does it already exist in Pharo, somewhere that Iâve failed to look? - Would it be a worthwhile addition to Pharo? - What would be a good name?
-- Guille Polito Research Engineer Centre de Recherche en Informatique, Signal et Automatique de Lille CRIStAL - UMR 9189 French National Center for Scientific Research - *http://www.cnrs.fr <http://www.cnrs.fr>* *Web:* *http://guillep.github.io* <http://guillep.github.io> *Phone: *+33 06 52 70 66 13