Global variables?
Jon Cast
jcast@ou.edu
Fri, 31 Jan 2003 13:54:26 -0600
Nick Name <nick.name@inwind.it> wrote:
> On Fri, 31 Jan 2003 07:47:43 +0000
> Glynn Clements <glynn.clements@virgin.net> wrote:
> > The usual fudge is:
> > import IORef
> > import IOExts
> > globalVar :: IORef Int
> > globalVar = unsafePerformIO $ newIORef 0
> I see in the documentation of unsafePerformIO that no one makes
> guarantees about the order in wich unsafePerformIO arguments are
> performed (especially they don't have to be executed BEFORE main), so
> this trick should not be adviced as a general practice; it should be
> pointed out that it works in GHC but could not work in other
> compilers.
English nit: I think you mean ``could fail in other compilers'': it's
not really impossible for the trick to work in other compilers than
GHC.
Otherwise, though, see my other post on this subject: unsafePerformIO
will perform its action when the variable is accessed, so you can't
write a Haskell program which differentiates between what any compiler
actually does and running the variable allocations before main.
> Vincenzo
> --
> Fedeli alla linea, anche quando non c' Quando l'imperatore
> malato, quando muore,o dubbioso, o perplesso. Fedeli alla linea
> la linea non c'. [CCCP]
Jon Cast