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



On Feb 2, 2010, at 11:07 AM, Henrik Johansen wrote:

> Scratch that. It's what I would have done with a fileNamed:do: selector :)
> 
> The main difference is readOnlyFileNamed:do: does not evaluate the block
> if file creation returns nil, while the second code errors out with
> stream dnu setConverterFromCode if the file could not be opened for any
> reason.
> 
> Cheers,
> Henry
> 
> Den 02.02.2010 11:02, skrev Henrik Johansen:
>> My guess is he uses a method which does not register the file in
>> FileRegistry, thus avoids a WeakDict cleanup when he closes the file
>> after the block has been evaluated.
>> (eg readOnlyFileNamed:do: vs. readOnlyFileNamed:)
>> Thus, a lot of time is saved from not doing weak registry cleanup.
>> (Which can be a large chunk of the total time when you do many small
>> operations).
>> 
>> Cheers,
>> Henry
>> 
>> Den 02.02.2010 10:50, skrev Stéphane Ducasse:
>> 
>>> Nicolas I saw that in squeak you changed and use readOnlyFileNamed:do:
>>> I like it but I would like to understand what is the key advantage
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> Pharo-project mailing list
>>> [email protected]
>>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>>> 
>>> 
>>> 
>>> 
>> _______________________________________________
>> Pharo-project mailing list
>> [email protected]
>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>> 
>> 
>> 
> 
> _______________________________________________
> Pharo-project mailing list
> [email protected]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


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

Reply via email to