Robert Dockins wrote:
On Apr 21, 2006, at 9:56 AM, Brian Hulley wrote:
Hi -
I've run into the global mutable state problem described in http://
www.haskell.org/hawiki/GlobalMutableState
Since the page was last edited in March last year, I'm wondering if
there have been any developments or further thoughts on how to
safely create top level IORefs since they are absolutely essential
for the library I'm writing.
For my library, which implements a GUI, I have a Manager module
which keeps track of which control currently has the keyboard focus
etc, and I don't want to have to pass round the state of the
manager to every control since this would be monstrously
inconvenient and a total waste of space/time, so at the moment I'm
reduced to:
module Manager where
keyboard :: IORef (Maybe Control)
{-# NOINLINE keyboard #-}
keyboard = unsafePerformIO $ newIORef Nothing
The problem is that I don't know if this is guaranteed to be
completely safe for all Haskell compilers or even for all future
versions of ghc (?)
RE: the technique itself, you should also compile the module with -
fno-cse.
Thanks
RE: the design, Isn't that bit of state local to a dialog/window/
control group or something? I understand that top level state is a
problem in general that needs some sort of solution, but I'm not sure
it's the right hammer here....
There is only one GUI for the application and only one control in it can
have the keyboard focus so it seems natural to use global state here, but I
suppose I could also look into using a state monad. The advantage (perhaps
also disadvantage ;-) ) of global state is that it allows me to easily
convert all my old C++ singleton classes to Haskell modules...
As far as I know, the only recent developments in this area are a
rumor from the Simons that they are working on some sort of thread-
local state which (under some sets of design decisions) can fill the
needs of top level state. If you press them, they might be willing
to give some details about this.
I was kind of hoping that there would just be a safe, simple way to create a
top level monomorphic IORef without having to use a pragma etc.
Thanks, Brian.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe