[Haskell-cafe] Is it safe to use unsafePerformIO here?

Cristiano Paris cristiano.paris at gmail.com
Tue Sep 15 15:41:04 EDT 2009

On Tue, Sep 15, 2009 at 9:39 PM, Svein Ove Aas <svein.ove at aas.no> wrote:
> As a general sort of warning, do not use hGetContents (or lazy i/o, in
> general) in combination with withFile.
> withFile closes the handle when the program lexically exits its scope.
> However, when using hGetContents, the file contents will not be read
> until after you do this, and will therefore fail to be read at all;
> I'm not sure whether this will produce a truncated string or an
> exception.

A truncated string. I already encountered such a scenario.

> Instead, use openFile directly. Handles have (ISTR) finalizers on
> them, and so should be automatically closed if you lose hold of one..
> eventually. getContents of course closes it once it hits EOF, but that
> isn't exactly reliable.
> If that isn't satisfactory, use strict I/O. It's less convenient, but
> it's also easier to reason about.

Thank you.


More information about the Haskell-Cafe mailing list