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.

_______________________________________________
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to