A way to avoid a common use of unsafePerformIO

前端 未结 5 841
天命终不由人
天命终不由人 2021-01-03 23:37

I often find this pattern in Haskell code:

options :: MVar OptionRecord
options = unsafePerformIO $ newEmptyMVar

...

doSomething :: Foo -> Bar
doSomethi         


        
5条回答
  •  无人及你
    2021-01-03 23:56

    If you are using MVar for holding settings or something similar, why don't you try reader monad?

    foo :: ReaderT OptionRecord IO ()
    foo = do
        options <- ask
        fireMissiles
    
    main = runReaderT foo (OptionRecord "foo")
    

    (And regular Reader if you don't require IO :P)

提交回复
热议问题