[Haskell] Re: Global Variables and IO initializers

Vincenzo Ciancia vincenzo_mlRE.MOVE at yahoo.it
Sat Nov 6 06:40:30 EST 2004

On Friday 05 November 2004 22:07, Keean Schupke wrote:
> myRef :: IORef Int
> myRef = unsafePerformIO $ newIORef 0
> This should always return the same reference, whereas:
> myIORef :: IO (IORef Int)
> myIORef = newIORef 0
> Will return a new reference every time. I agree it would seem that
> the first form does not need to be in the IO monad as it is
> effectively a constant.

Yes, but I guess everybody would like a solution where

myRef1 = unsafePerformIO $ newIORef 0
myRef2 = unsafePerformIO $ newIORef 0

are different variables. Also, it's not true that it's perfectly safe, 
we are just assuming here that all the global values are evaluated 
before the main action, which should not be so obvious, if main is 
executed before the two unsafePerformIO actions we will get undefined 
behaviour, that's why newIORef has an "IO" type after all. 


More information about the Haskell mailing list