> Hi norbert
>
> >> I have a question:
> >> - would not be better to even wrap the on: into the
> first block of
> >> the ensure: message?
> > What do you mean? Can you give an example?
>
> In VW I was used to write something like that from memory
>
> [ f := 'foo.bar' asFilename.
> f open....
>
>
> ] ensure: [f close]
>
> so the file creation was protected.
If a file fails to open for whatever reason the result will be nil, if you
protect the open, then you'll end up trying to close nil during the ensure.
Nil is the failure message for Squeaks file operations, exceptions are not
thrown.
fileNamed: aName do: aBlock
| file | file := self fileNamed: aName.
^ file
ifNil: [ nil ]
ifNotNil: [ [ aBlock value: file ] ensure: [ file close ] ]
Seems correct to me, you only need the ensure if the file opened
successfully in which case file won't be nil.
Ramon Leon
http://onsmalltalk.com
_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project