Discussion: Add safeWithFile function
Oliver Charles
ollie at ocharles.org.uk
Sun Oct 12 15:58:02 UTC 2014
Where are you suggesting this library goes? Your `safeWithFile` needs
`NFData`, but that's in `deepseq` which isn't a dependency of `base`.
- ocharles
On Sat, Oct 11, 2014 at 10:10 PM, David Feuer <david.feuer at gmail.com> wrote:
> Some care is required to use withFile properly with lazy IO, and people
> sometimes get it wrong. In some cases, readFile is a suitable replacement.
> In other cases, however, it may be necessary to ensure the file gets closed
> in a timely manner, and it may not be necessary to read the whole file. The
> following seems to do the trick:
>
> safeWithFile :: (NFData a) => String -> IOMode -> (Handle -> IO a) -> IO a
> safeWithFile name mode f = withFile name mode $ f >=> (return $!!)
>
>
> Sample use:
>
> *SafeWithFile> safeWithFile "SafeWithFile.hs" ReadMode hGetContents >>=
> putStr
> module SafeWithFile (safeWithFile) where
> import Control.DeepSeq
> import System.IO
> import Control.Monad
>
> safeWithFile :: (NFData a) => String -> IOMode -> (Handle -> IO a) -> IO a
> safeWithFile name mode f = withFile name mode $ f >=> (return $!!)
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20141012/6dafe024/attachment.html>
More information about the Libraries
mailing list