Global variables?
Ralf Hinze
ralf@informatik.uni-bonn.de
Fri, 31 Jan 2003 09:08:22 +0100
> Pavel G. Zhbanov wrote:
> > Is it even possible to make a "global variable" in Haskell?
> > If yes, how?
>
> The usual fudge is:
>
> import IORef
> import IOExts
>
> globalVar :: IORef Int
> globalVar = unsafePerformIO $ newIORef 0
>
> However, beware of creating more than one "global variable" of the
> same type. If you enable optimisation, common subexpression
> elimination may result in both names referring to the same IORef.
John Hughes wrote a nice pearl on the subject, see
http://www.math.chalmers.se/~rjmh/Globals.ps
Cheers, Ralf