[Haskell-cafe] Automatic file closing after readFile

Magnus Therning magnus at therning.org
Thu Oct 18 09:48:04 EDT 2007


On Thu, Oct 18, 2007 at 09:25:32 -0400, David Roundy wrote:
>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
>Control.Exception.

Yes, thankfully that's documented in the Haddock for the only one that
turns up on Hoogle.

/M

-- 
Magnus Therning                             (OpenPGP: 0xAB4DFBA4)
magnus@therning.org             Jabber: magnus.therning@gmail.com
http://therning.org/magnus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071018/964409ac/attachment-0001.bin


More information about the Haskell-Cafe mailing list