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

Svein Ove Aas svein.ove at aas.no
Tue Sep 15 15:39:17 EDT 2009

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

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.

Svein Ove Aas

More information about the Haskell-Cafe mailing list