Sockets and finalisation
Avi Bryant
avi at beta4.com
Thu Jan 22 21:59:40 UTC 2004
On Jan 22, 2004, at 1:42 PM, Andreas Raab wrote:
> Therefore, if you write an app which uses resource-allocating objects,
> it is
> YOUR task to make sure they are closed properly. Which is one of the
> reasons
> why patterns such as:
> file := self openFile: 'foo.txt'
> [self doSomethingWith: file] ensure:[file close].
> are used. IOW, the above ensures that the opened file is indeed
> properly
> closed after we are finished with it, finalization or not.
Just out of curiosity - given this, why does FileStream not have
methods something like
FileStream class>>fileNamed: fileName do: aBlock
|file|
file := self fileNamed: fileName.
[aBlock value: file] ensure: file close
This is quite common in other languages that make heavy use of
closures, ie Lisp (with-file 'foo' (f) ...) and Ruby
(File.open('foo'){|f|...}), but for some reason it doesn't seem to be
idiomatic in Smalltalk.
Avi
More information about the Squeak-dev
mailing list
|