[Haskell-cafe] about openTempFile

Don Stewart dons at galois.com
Wed Sep 17 11:28:57 EDT 2008


manlio_perillo:
> Dougal Stanton ha scritto:
> >On Wed, Sep 17, 2008 at 1:17 PM, Manlio Perillo
> ><manlio_perillo at libero.it> wrote:
> >>The Python tempfile module, as an example, implements a wrapper around
> >>mkstemp function that does exactly this, and the code is portable; on
> >>Windows it uses O_TEMPORARY_FILE flag, on POSIX systems the file is
> >>unlink-ed as soon as it is created (but note that the code is not signal
> >>safe - well, many functions in the Python standard library are not signal
> >>safe).
> >
> >Something like:
> >
> >>withTempFile :: String -> ((FilePath, Handle) -> IO b) -> IO b
> >>withTempFile name action = do
> >>    tmpdir <- getTemporaryDirectory
> >>    bracket
> >>        (openTempFile tmpdir name)
> >>        (\(fp,h) -> hClose h >> removeFile fp)
> >>        action
> >
> >I don't know if this has the safety requirements you mean?
> >
> 
> Yes, this is the ideal solution for Haskell.
> 
> But since GHC does not implement such a function, I was curious to know 
> why it don't even implement a function that make sure the temporary file 
> is removed.
> 

Because it is two lines to write, so no one has yet proposed it for the
base library. 

-- Don


More information about the Haskell-Cafe mailing list