unsafePerformIO and IORefs

Simon Marlow simonmar@microsoft.com
Tue, 19 Nov 2002 10:53:28 -0000


> At 2002-11-18 11:05, Sven Panne wrote:
>=20
> >global :: a -> IORef a
> >global a =3D unsafePerformIO (newIORef a)
>=20
> This is useful, you can do this with it:
>=20
>   ref =3D global Nothing
>=20
>   convert :: a -> IO b
>   convert a =3D do
>     writeIORef ref (Just a)
>     Just b <- readIORef ref
>     return b

This particular "flexibility" provided by unsafePerformIO is actually
documented...

http://www.haskell.org/ghc/docs/latest/html/base/System.IO.Unsafe.html#u
nsafePerformIO

Cheers,
	Simon