Global variables via unsafePerformIO in Haskell

岁酱吖の 提交于 2019-12-01 22:16:45
ShiDoiSi

See this answer. It shows how to use a global counter that 'ticks' everytime you look at it. You don't need a counter, but instead of +1, you just put True into it.

Or, even better, you put the initialisation code into the unsafePerformIO, (guarded by an if of course).

You need to turn inlining off. The other important thing: The type has to be monomorph (= no type variables), otherwise you might evaluate unsafePerformIO for each actual type.

{-# NOINLINE ghcInitialised #-}
ghcInitialised :: MVar (Bool,[String])
ghcInitialised = unsafePerformIO $ newMVar (False,[])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!