[Haskell-cafe] Re: Is it safe to use unsafePerformIO here?
ben.franksen at online.de
Sat Sep 19 12:53:52 EDT 2009
Cristiano Paris wrote:
> Daniel Fischer wrote:
>> I would separate the reading of headers and bodies, reopening the files
>> whose body is needed, for some (maybe compelling) reason he wants to do
>> it differently.
> Yes, that's the way Haskell forces you to do that as it's the only way
> for you to go safe.
I don't think it has anything to do with Haskell. How would you do this in
C? You'd pass a flag indicating whether to read the whole file or just the
header. You can do the same in Haskell, of course, no lazy IO needed. The
body remains undefined if the flag indicates header only. Even better wrap
the body in a Maybe.
> But, if you know more about your code, you can use
> unsafe(Perform|Interleave)IO to assure the compiler that everything's
I have a hard time believing this is possible, if you demand that the files
should not stay opened indefinitely. How is the runtime supposed to know
whether to close the file or not? What you /can/ do is use unsafePerformIO
to lazily re-open, read the body, and close the file, as soon as the body
gets demanded. However, this is ugly and not advised.
More information about the Haskell-Cafe