[Haskell-cafe] Re: Getting WriterT log lazily
es at ertes.de
Thu May 7 12:37:20 EDT 2009
Stephen Hicks <stephenhicks at gmail.com> wrote:
> Magnus Therning wrote:
> > Martijn van Steenbergen wrote:
> >> Otherwise, you can use unsafeInterleaveIO: no unsafePerformIO or seq
> >> needed, but there's still "unsafe" in that name there. This works for me:
> > Thanks, that does indeed work, but it still requires that "unsafe" there so
> > I'm hesitant replacing the call to threadDelay with something more
> > complicated, where it isn't obviously safe.
> Sorry for the late reply, but I'd like to piggy-back on this. It's my
> understanding that unsafeInterleaveIO is only unsafe insofar as it
> makes the IO lazy and so the IO may be performed later or never at all
> (if its return value is never wanted) and is therefore no less safe
> than, say, generating a list of IO actions as was suggested by an
> earlier reply. It seems to me that its unsafeness is of a completely
> different nature than unsafePerformIO... am I missing something?
There are two issues with unsafePerformIO: The laziness makes
evaluation unpredictable to some extent, but that's intentional. But
for example what if the IO computation throws an exception? There is no
well-defined behaviour, not even a well-known convention. For example,
hGetContents simply aborts, which makes errors undetectable.
All in all, unsafeInterleaveIO is a handy tool, but you need to be
careful about its side effects.
nightmare = unsafePerformIO (getWrongWife >>= sex)
More information about the Haskell-Cafe