[Haskell-cafe] Re: [Haskell] Re: state of HaXml?

Donald Bruce Stewart dons at cse.unsw.edu.au
Thu Jan 4 21:09:22 EST 2007

>  > Sure, you can replace the openFile/hGetContents pair by readFile, but the
>  > real problem is the presence of the hClose.  Removing that will solve your
>  > problem (but note that you now have no control over when the file is
>  > actually closed).
> Can I just leave it hanging and rely on the garbage collector to close
> it in the fullness of time?

Yeah, once your program has demanded the entire file, it'll close the
> Because of laziness, I believe there's no point in my writing the
> following:
>  >    load fn = do handle <- IO.openFile fn IO.ReadMode
>  >                 contents <- IO.hGetContents handle
>  >                 let xml = XP.xmlParse fn contents
>  >                 IO.hClose handle
>  >                 return xml
> Is that correct?

Yep.  Its not neccessary in the usual programming cases to explicitly
close the handle.

IF you start really hammering the filesystem do you start to care about
ensuring files are closed (so you don't hang on to too many FDs). Or if
you start mutating files on disk. For these situations there are strict
readFiles, or Data.ByteString.readFile

-- Don

More information about the Haskell-Cafe mailing list