unsafePerformIO to give warnings

Ian Lynagh igloo@earth.li
Thu, 20 Dec 2001 21:49:03 +0000

Hi all

If I want to give warnings when doing something and I don't care too
much about the order they appear in, can I use this?

    foo x = if success x then Just x
                         else warn "Working out x went wrong" Nothing

    warn :: String -> a -> a
    warn s x = unsafePerformIO (hPutStrLn stderr s) `seq` x

The hslibs docs say

    If the I/O computation wrapped in unsafePerformIO performs side
    effects, then the relative order in which those side effects take
    place (relative to the main I/O trunk, or other calls to
    unsafePerformIO) is indeterminate.

but it's not entirely clear on whether or not I could end up with 2
warnings interspersed?

And is it guaranteed that the warnings will be printed at some point?
hugs and ghci only seem to print the first warning, but ISTR similar
problems happen with threads and got the impression that they were in
the wrong.