[Haskell-cafe] One-shot? (was: Global variables and stuff)

Keean Schupke k.schupke at imperial.ac.uk
Wed Nov 10 18:36:47 EST 2004


Hi,
    Here's another completely safe (and simpler way) to limit
a computation to only happen once:

    once' :: IO () -> IO ()
    once' f = do
       k <- getProcessID
       a <- getEnv (showString "MyApp.Main" $ show k)
       case a of
          Just _ -> return ()
          _ -> do
             f
             setEnv (showString "MyApp.Main" $ show k) "" False

Actually both this and the semaphore example show that there is probably
an alternative to allowing top-level '<-' type definitions - and that 
would be
to have named-MVars in Haskell. It would be quite easy to code these as a
small C library - and then FFI import the bindings to Haskell. I don't know
whether from a 'purists' point of view whether this represents anything 
better
than module-initialisations - but it does remove the diamond-inheritance 
style
problem you get (if B imports A and C imports A and Main imports B and C,
does A's init get run twice? are there two copies of the variables, each 
initialised
once, or one copy that gets initialised twice?). But either way the idea 
could
be implemented without requiring changes to the language spec or the
compilers, and would just be a library which you could use.

    Keean.



More information about the Haskell-Cafe mailing list