[Haskell] Initialisation without unsafePerformIO

John Meacham john at repetae.net
Tue Jun 1 12:35:15 EDT 2004


On Tue, Jun 01, 2004 at 06:06:36PM +0200, George Russell wrote:
> What ideas do people have for getting rid of unsafePerformIO?
> 
> The most common use of unsafePerformIO, for me at least, is initialisation.
> There *surely* must be a better way of doing this, but I haven't really
> seen much discussion of the topic.  Here is my back-of-the-envelope
> suggestion for a new interface, can anyone do better?

I am a fan of allowing top level declarations of the form:

foo <- newIORef "foo"

which would behave as an initializer, with the semantics being that it
be evaluated at most once before foos first use. (so it could be
implemented via unsafePerformIO or as an init section run before main).

The 
{-# NOINLINE foo #-}
foo = unsafePeformIO $ newIORef "foo"

idiom is so common and useful, it should have some compiler support. It
is 'clean' too, since all we are doing is extending the "world" with new
state, but in a much cleaner/safer way then writing to a file or environment
variable or other methods of storing state in the world.

        John
-- 
John Meacham - ⑆repetae.net⑆john⑈ 


More information about the Haskell mailing list