HN2new | past | comments | ask | show | jobs | submitlogin

  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.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: