top | item 7587341

(no title)

chrra | 12 years ago

  newtype X = X (IORef Int)
  instance Num X where
    fromInteger = X . unsafePerformIO . newIORef . fromInteger
  instance Eq X where
    (X a) == (X b) = unsafePerformIO $ do
                       x <- readIORef a
                       y <- readIORef b
                       writeIORef a $ x+1
                       return $ x == y

  ghci> a <- fmap X $ newIORef 1
  ghci> a == 1 && a == 2
  True
Edit: formatting.

discuss

order

No comments yet.