[Haskell] IORef sharing

Rodney D Price rodprice at RAYTHEON.COM
Mon Oct 27 19:02:54 EDT 2008


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?

-Rod



On Oct 27, 2008, at 4:36 PM, Martijn van Steenbergen wrote:

> Rodney D Price wrote:
>>

>> So apparently my mental picture of an IORef as a pointer
>> to a value is wrong.  I need a new mental picture.  What's
>> going on here?
>
> Naming the creation of a new IORef "iio" is not gonna make it return  
> the same IORef every time you call iio. Imagine iio being expanded  
> to its definition in your counter1 example (which is exactly what  
> happens); would you still expect the result to be 1,1,2,3? If you  
> want the IORef to be shared by different pieces of code, pass it  
> around as an argument.
>
> Kind regards,
>
> Martijn.



More information about the Haskell mailing list