
Hi, by trying to understand how "writeIORef" works, I looked at data STRef s a = STRef (MutVar# s a) what is MutVar# ? (especially zhe "#" ) ? I've readed tha writeIORef (and readIoRef etc) is used to write directly to memory places fo implementing variables, as in example in HOpenGL used. Is this also Haskell standard ? Thanks for help Hans

Hans Nikolaus Beck wrote:
[...] I've readed tha writeIORef (and readIoRef etc) is used to write directly to memory places fo implementing variables, as in example in HOpenGL used. Is this also Haskell standard ?
Huh? I'm not sure what you mean exactly, but with the help of unsafePerformIO and a pragma for clever compilers you can simulate something like a global variable in Haskell. Here an excerpt from the GLUT menu handling module: {-# NOINLINE theMenuTable #-} theMenuTable :: IORef MenuTable theMenuTable = unsafePerformIO (newIORef emptyMenuTable) getMenuTable :: IO MenuTable getMenuTable = readIORef theMenuTable modifyMenuTable :: (MenuTable -> MenuTable) -> IO () modifyMenuTable = modifyIORef theMenuTable Definitely not something to be proud of, but quite handy from time to time. :-) Alternatives are passing the IORef to every function which needs it (but this would clutter up the GLUT API in the above case) or using the FFI for holding global data on the C side. GHC uses a similar hack internally, too, BTW. Cheers, S.
participants (2)
-
HNBeck@t-online.de
-
Sven Panne