[Haskell-cafe] unsafeDestructiveAssign?

Roberto Zunino zunino at di.unipi.it
Thu Aug 13 09:02:44 EDT 2009


Job Vranish wrote:
> Does anybody know if there is some unsafe IO function that would let me do
> destructive assignment?
> Something like:
> 
> a = 5
> main = do
>   veryUnsafeAndYouShouldNeverEveryCallThisFunction_DestructiveAssign a 8
>   print a
>> 8

Untested, just guessing:

   {-# NOILINE aRef , a #-}
   aRef = unsafePerformIO (newRef 0)
   a = unsafePerformIO aRef
   main = do
      use a    -- 0 if you are lucky
      writeRef aRef 5
      use a    -- 5 if you are lucky

However, I would not in the least be surprised if your program stops 
working whenever your cat purrs.

And yes, I find the above "code" quite disgusting.

Zun.


More information about the Haskell-Cafe mailing list