[SOLVED] Safe elimination of `unsafePerformIO`?

As @Ambrose pointed out, this is an idiom for global mutable variables. Answering your questions in case it helps understand what is going on:

In this case, unsafePerformIO is used to create a single MVar for the top-level declaration of entrypointMVar. The NOINLINE is critical because it would instead create a new MVar for each call to entrypoint if it were inlined. The RIO env (MVar Bool) version creates a new MVar for each call to entrypoint, so it is not the same as the existing code.

Ignoring the Stan warning should be fine. If you prefer to avoid using unsafePerformIO, you can refactor the code so that the MVar is included in the env and initialize it (using MonadIO) when the env is initialized. The above refactored code is not a safe refactoring.

5 Likes