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

Cristiano Paris frodo at theshire.org
Thu Sep 17 15:07:28 EDT 2009


On Tue, Sep 15, 2009 at 11:31 PM, Daniel Fischer
<daniel.is.fischer at web.de> wrote:
> ...
> Yeah, you do *not* want the whole file to be read here, except above for testing purposes.

That's not true. Sometimes I want to, sometimes don't. But I want to
use the same code for reading files and exploit laziness to avoid
reading the body.

> Still, ByteStrings are probably the better choice (if you want the body and that can be
> large).

That's not a problem by now.

> To avoid reading the body without unsafePerformIO:
>
> readBit fn
>    = Control.Exception.bracket (openFile fn ReadMode) hClose
>          (\h -> do
>                l <- hGetLine h
>                let i = read l
>                bdy <- hGetContents h
>                return $ Bit i bdy)

Same problem with the "withFile"-version: nothing gets printed if I
try to print out the body: that's way I used seq.

I'm starting to think that the only way to do this without using
unsafePerformIO is to have the body being an IO action: simply, under
Haskell assumption, that's not possible to write, because Haskell
enforce safety above all.

Cristiano


More information about the Haskell-Cafe mailing list