[Haskell-cafe] why cannot i get the value of a IORef variable ?

Thomas DuBuisson thomas.dubuisson at gmail.com
Thu Oct 22 13:41:39 EDT 2009

> For clarity, one trick that uses "unsafePerformIO" which you may have seen
> posted on this list earlier today is the following way of creating a
> globally visible IORef:
> import Data.IORef
> import System.IO.Unsafe
> *** counter = unsafePerformIO $ newIORef 0 ***
> next = do
>  modifyIORef counter (+1)
>  readIORef counter

This is still unsafe but it can evidently be slightly improved with
NOINLINE pragma:

    {-# NOINLINE counter #-}
    counter = unsafePerformIO $ newIORef 0

without said pragma the counter could be initialized numerous times,
as I understand it.  All this said, I hope people avoid
unsafePerformIO for mutable globals - reader monad, state monad, and
partial application should be sufficient tools.


More information about the Haskell-Cafe mailing list