blob: d314ed428cf68c487933e70b1966f7e3f98b7448 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Data.IORef (IORef, atomicModifyIORef', newIORef)
import InitOnce
globalRef :: IORef Int
globalRef = $$(once [|| newIORef 0 ||])
-- wrong :: Typeable a => IORef [a] -- does not typecheck: no instance Typeable a
-- wrong = $$(once [|| newIORef [] ||])
{-# NOINLINE foo #-}
foo :: IO String
foo = atomicModifyIORef' globalRef (\i -> (i + 1, show i))
main :: IO ()
main = do
putStrLn =<< foo -- 0
putStrLn =<< foo -- 1
|