A way to avoid a common use of unsafePerformIO

前端 未结 5 836
天命终不由人
天命终不由人 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:44

    Those who would trade essential referential transparency for a little temporary convenience deserve neither purity nor convenience.

    This is a bad idea. The code that you're finding this in is bad code.*

    There's no way to fully wrap this pattern up safely, because it is not a safe pattern. Do not do this in your code. Do not look for a safe way to do this. There is not a safe way to do this. Put the unsafePerformIO down on the floor, slowly, and back away from the console...

    *There are legitimate reasons that people do use top level MVars, but those reasons have to do with bindings to foreign code for the most part, or a few other things where the alternative is very messy. In those instances, as far as I know, however, the top level MVars are not accessed from behind unsafePerformIO.

提交回复
热议问题