proposed addition to System.IO

Donald Bruce Stewart dons at cse.unsw.edu.au
Mon Oct 23 19:53:27 EDT 2006


ross:
> I propose adding the following functions to System.IO:
> 
> -- | @'withFile' name mode act@ opens a file using 'openFile' and passes
> -- the resulting handle to the computation @act at .  The handle will be
> -- closed on exit from 'withFile', whether by normal termination or by
> -- raising an exception.
> withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
> withFile name mode = bracket (openFile name mode) hClose
> 
> -- | @'withBinaryFile' name mode act@ opens a file using 'openBinaryFile'
> -- and passes the resulting handle to the computation @act at .  The handle
> -- will be closed on exit from 'withBinaryFile', whether by normal
> -- termination or by raising an exception.
> withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
> withBinaryFile name mode = bracket (openBinaryFile name mode) hClose
> 
> (bracket is the one from Control.Exception, if available, else the one
> from System.IO.Error)
> 
> These functions are close to the Fairbairn Threshold, but just above it,
> I think, and worth naming because they wrap up a pattern that should be
> encouraged.

Yes, I agree. We could do with these in Data.ByteString too.

+1

-- Don


More information about the Libraries mailing list