[Haskell-beginners] StateT MyState IO a vs ReaderT (IORef MyState) IO a

Grzegorz Milka grzegorzmilka at gmail.com
Sat Apr 18 19:10:00 UTC 2015


ReaderT (IORef MyState) IO a is introducing a global mutable state into
your IO actions. This style is discouraged, because you have less
guarantees about what can happen with your state. For example IORef is
not protected from multi-threaded access. If you try to use this state
in multiple threads you may run into bugs. StateT MyState IO a doesn't
have that problem. For a typical use-case, that is an action that: reads
state, performs action, modifies state, both are similar and neither
should be significantly more comfortable. I would recommend to use
StateT version unless you find that either it does not fit your model of
computation or that it is underperforming.

As for the performance it is hard to say. StateT allows the compiler to
reason more about your code, so it might optimize better. However that's
only one factor. You would need to test it to find out which one is better.



More information about the Beginners mailing list