
22 Oct
2009
22 Oct
'09
1:41 p.m.
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. Thomas