[Haskell-cafe] On hGetContents semi-closenesscloseness

Ketil Malde ketil at malde.org
Wed Feb 16 09:52:26 CET 2011

Brandon S Allbery KF8NH <allbery.b at gmail.com> writes:

>> openFile "foo" ReadMode >>= \handle -> (hGetContents handle >>= (\s -> hClose handle >> putStr s)) [2]

> This is a classic example of the dangers of hGetContents (and, more
> generally, of unsafeInterleaveIO).  

Which makes me wonder why it isn't an error to hClose a semi-closed
handle as well?  The reason that springs to mind is that most systems
(or at least Linux) has for some unfathomable reason an arbitrary, low,
and fixed limit on the number of open files, and it is therefore
sometimes necessary or desirable to close files before opening new

> In general, you should use lazy I/O only for "quick and dirty" stuff
> and avoid it for serious programming.  

I must admit that I use lazy I/O all the time - usually in the form of
'readFile' rather than 'hGetContents'.

> You can get many of the benefits of lazy I/O without the
> nondeterminacy by using iteratee-based I/O
> (http://hackage.haskell.org/package/iteratee). 

I think it is fair to say that iteratees are a bit more involved.

If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell-Cafe mailing list