On 02.02.2010 18:35, Stéphane Ducasse wrote:
> Hi henrik
>
> this is for fileStream
> and I like the idea that this is more compact now reading the code of
>
> fromFileNamed: aName
>       FileStream readOnlyFileNamed: aName do: [:stream |
>                stream setConverterForCode.
>                self fileInFrom: stream]
>
> readOnlyFileNamed: fileName do: aBlock
>       
>       ^ self detectFile: [ self readOnlyFileNamed: fileName ] do: aBlock
>
> detectFile: aBlock do: anotherBlock
>       
>       | file |
>       file := aBlock value.
>       ^ file
>               ifNil: [ nil ]
>           ifNotNil: [ [anotherBlock value: file] ensure: [file close]]
>
> It is less obvious to me that the ensure: will really close the file
> if readOnlyFileNamed: fails.
>
> Before it was clearly not the case
> fromFileNamed: aName
>        | stream |
>        stream := FileStream readOnlyFileNamed: aName.
>        stream setConverterForCode.
>        [self fileInFrom: stream] ensure: [stream close].
>
> so I would have written it
> fromFileNamed: aName
>        | stream |
>        [stream := FileStream readOnlyFileNamed: aName.
>        stream setConverterForCode.
>        self fileInFrom: stream] ensure: [stream close].
>
> what do you think?
>
> Stef
>    
Not really.
Either there's no error when you request the file be opened, and the 
block works, or you don't, and there's nothing to close.
So as long as the block meant to open a file really does nothing else 
but try to open a file, there's nothing wrong with the scope of the 
ensure guard.
If you want to be able to use the detectFile:do: to write code like:
detectFile: [|fileStream|
                 fileStream := FileStream newFileNamed: 'test.txt'.
                 fileStream encoder: ISO88592TextConverter new.
                  fileStream] do: [:aStream doStuff]
the expanded guard would possibly be needed, to me that's giving aBlock 
too much responsibility though, I'd rather it'd be forced written:
FileStream detectFile: [FileStream newFileNamed: 'test.txt']
                    do: [:aStream encoder: ISO88592TextConverter new; 
doStuff]

Cheers,
Henry

_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply via email to