[Haskell] IORef sharing

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Mon Oct 27 19:16:08 EDT 2008

On 2008 Oct 27, at 19:02, Rodney D Price wrote:
> My old, deeply flawed mental picture had "iio" taking
> the role of a pointer to a value.  My bright, shiny
> new mental picture has "iio" acting just like a C
> #define macro: every time I call "iio", I'm really
> just writing "newIORef 0".  Is that what you're saying?

Sort of.  What's really happening is that "newIORef" is an I/O action,  
just like "putStrLn", so will be executed every time it's used just as  
"putStrLn" is.

If you want to do it only once, you must do it only once and pass the  
resulting IORef around.  (There is also a way to do "global  
variables", but it's rather unsafe and requires telling the compiler  
to not rewrite the initialization code.)

brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

More information about the Haskell mailing list