This is how it should be done: readStreamDo: aBlock | stream | stream := self readStream. ^ [ aBlock value: stream ] ensure: [ stream close ] Errors while opening or closing are still signalled, which is good IMO.
On 30 Nov 2014, at 11:18, Thierry Goubier <thierry.goubier@gmail.com> wrote:
Le 30/11/2014 11:06, Max Leske a écrit :
The code would look like this:
[ | file | file := StandardFileStream forceNewFileNamed: âfooâ. file nextPutAll: âbarâ ] ensure: [ file close ].
Why that is considered bad practice however, I canât tell.
This one would fail because file is a temp to the block (and not visible to the ensure block).
Another problem is that, if you have a failure in forceNewFileNamed:, file is nil and the ensure: fail as well.
This is how I interpret that rule.
Thierry