I’ve noticed this idiom in Data.Unique:
uniqSource :: TVar Integer
uniqSource = unsafePerformIO (newTVarIO 0)
{-# NOINLINE uniqSource #-}
Is it guaranteed to only run once?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In GHC, yes.1 See the documentation for more information; there is a variant
unsafeDupablePerformIOthat can be executed multiple times that avoids the overhead dedicated to achieving this guarantee.Note that
unsafePerformIOto create mutable variables isn’t safe in general; as described in the documentation, you can create a polymorphic reference and use it to implementunsafeCoerce. That’s not something you’re likely to do accidentally, though, and it doesn’t apply to the code in question (since the type of the reference is specified explicitly).The safe-globals package abstracts this “idiom” (while useful in some cases, it’s generally considered an antipattern, and should not be used in normal code) in a way that ensures safety.
See also my previous answer on
unsafePerformIOand the caution that must be used when applying it.1 I’m pretty sure it applies to all other implementations, too; the special care GHC takes to avoid repeated execution is only necessary in a threaded setting, and I don’t know of any other threaded implementations of Haskell. GHC is the only implementation people really use these days, though…