[Haskell-cafe] Automatic file closing after readFile

David Roundy droundy at darcs.net
Thu Oct 18 09:25:32 EDT 2007

On Thu, Oct 18, 2007 at 01:16:37PM +0100, Magnus Therning wrote:
> On Thu, Oct 18, 2007 at 20:58:45 +1000, Matthew Brecknell wrote:
> >For a less hackish solution, you need to do a bit more work. Again, this
> >is untested.
> >
> >> loadAndCheck fn = bracket (openFile fn ReadMode) hClose checkContents
> >> checkContents fh = do { s <- hGetContents fh; return $! hasEmpty s }
> >
> >Note the explicit close, and "return $!" to ensure that hasEmpty has
> >done its work before the file is closed.
> Ah, `bracket` is a handy function to know!

Note that you almost never want to use the bracket defined in Haskell98,
nor the one in IO.Error.  You want the bracket defined in
David Roundy
Department of Physics
Oregon State University
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071018/86e15a03/attachment.bin

More information about the Haskell-Cafe mailing list