On 07/01/14 03:21, Brandon Allbery
wrote:
You don't have to go that far though, if you want a 'global' IORef
then you simply make it at the top of your program and then pass it
around to anyone who needs access it to:
main :: IO ()
main = do
counter <- newIORef 0
doThingsWithCounter counter
doThingsWithCounter :: IORef Int -> IO ()
doThingsWithCounter counter = atomicModifyIORef
counter $
\x
->
let
y =
x +
1
in
(y,
y)
So this gives you a way to have global variables,
but without the pain that they can bring in other languages.
The Reader monad stuff that Brandon suggests is a way to implicitly
have this IORef passed around everywhere, which can be useful if you
have deeply nested calls where only the children really need access
to the IORef - it saves you having to add a new parameter
everywhere.
- ocharles