FW: lazy file reading in H98

Olaf Chitil olaf@cs.york.ac.uk
Sun, 08 Apr 2001 19:12:10 +0100


Marcin 'Qrczak' Kowalczyk wrote:
> 
> Sat, 07 Apr 2001 20:35:52 +0100, Olaf Chitil <olaf@cs.york.ac.uk> pisze:
> 
> >   do
> >     handle <- openFile "configuration" ReadMode
> >     contents <- hGetContents
> >     ...
> >     hClose handle
> >     writeFile "configuration" newContents
> 
> It will be wrong if the contents is not fully evaluated at the point
> of hClose.

What do you mean by `wrong'? As 11.2.2 says:
Once a semi-closed handle becomes closed, the contents of the associated
stream becomes fixed, and is the list of those items which were
successfully read from that handle. 
So only if you continue evaluating `contents' after `hClose' *and*
expect to get the full remaing file contents, then you have a problem.

> Perhaps hClose should arrange to performGC and suck the rest of the
> file if it's still needed.

This would be bad, if you still had a reference to `contents' and were
not interested in the rest of the file (seems to be the case in Manuel's
example). Also, a full garbage collection is quite expensive.

Simon doesn't want to make changes to Haskell. My opinion is that the
current definition of readFile and hGetContents is fine. They are useful
when used with a bit of care. Some remarks should make the reader of the
report aware of the caveats.
(I'm not saying that I couldn't imagine some useful additional functions
for lazy reading and writing.)

Olaf

-- 
OLAF CHITIL, 
 Dept. of Computer Science, University of York, York YO10 5DD, UK. 
 URL: http://www.cs.york.ac.uk/~olaf/
 Tel: +44 1904 434756; Fax: +44 1904 432767