[Haskell-cafe] Is unsafePerformIO safe here?

John Ky newhoggy at gmail.com
Sun Dec 7 20:52:13 EST 2008


Does that mean there is no place to store state while running the
interpreter and that I have to put the state elsewhere such as a file?  I
was hoping to avoid that as I'm only prototyping at this stage and don't
want to write a persistent layer just yet.

Thanks

-John

On Mon, Dec 8, 2008 at 11:51 AM, Thomas Davie <tom.davie at gmail.com> wrote:

>
> On 8 Dec 2008, at 01:28, John Ky wrote:
>
>  Hi,
>>
>> Is the following safe?
>>
>> moo :: TVar Int
>> moo = unsafePerformIO $ newTVarIO 1
>>
>> I'm interested in writing a stateful application, and I wanted to start
>> with writing some IO functions that did stuff on some state and then test
>> them over long periods of time in GHCi.
>>
>> I was worried I might be depending on some guarantees that aren't actually
>> there, like moo being discarded and recreated inbetween invocations of
>> different functions.
>>
>
> Define safe... In this case though, I would guess it's not safe.  The
> compiler is free to call moo zero, one or many times depending on its
> evaluation strategy, and when it's demanded.  It's possible that your TVar
> will get created many times, and different values returned by the "constant"
> moo.
>
> That sounds pretty unsafe to me.
>
> Bob
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081208/5140c0c5/attachment.htm


More information about the Haskell-Cafe mailing list