How do record updates behave internally?

Posted by redxaxder on Stack Overflow See other posts from Stack Overflow or by redxaxder
Published on 2011-11-30T15:59:31Z Indexed on 2011/11/30 17:50 UTC
Read the original article Hit count: 159

Filed under:
data Thing = Thing {a :: Int, b :: Int, c :: Int, (...) , z :: Int} deriving Show

foo = Thing 1 2 3 4 5 (...) 26
mkBar x = x { c = 30 }

main = do print $ mkBar foo

What is copied over when I mutate foo in this way? As opposed to mutating part of a structure directly.

Data Thing = Thing {a :: IORef Int, b :: IORef Int, (...) , z :: IORef Int}
instance Show Thing where
(...something something unsafePerformIO...)

mkFoo = do a <- newIORef 1
           (...)
           z <- newIORef 26
           return Thing a b (...) z
mkBar x = writeIORef (c x) 30

main = do foo <- mkFoo
          mkBar foo
          print foo

Does compiling with optimizations change this behavior?

© Stack Overflow or respective owner

Related posts about haskell