How to deal with application state in Gtk2Hs

前端 未结 1 1006
广开言路
广开言路 2020-12-06 02:15

Trying to learn to write applications with Gtk2Hs I\'m getting difficulties bridging the gap between the event driven Gtk2HS and the persistent state of my model. So to simp

相关标签:
1条回答
  • 2020-12-06 02:33

    There's basically two approaches:

    1. Use a pointer of some kind. This is your IORef or MVar approach. You can hide this behind a MonadState-like interface if you like:

      newtype GtkT s m a = GtkT { unGtkT :: ReaderT (IORef s) m a } deriving (Functor, Applicative, Monad, MonadIO)
      runGtkT = runReaderT . unGtkT
      
      instance MonadIO m => MonadState s (GtkT s m) where
          get   = GtkT (ask >>= liftIO . readIORef)
          put s = GtkT (ask >>= liftIO . flip writeIORef s)
      
    2. Pull an "inversion of control" style trick. Write a callback that prints a number, then replaces itself with a new callback that prints a higher number.

    If you try to use State or StateT directly, you're gonna have a bad time.

    0 讨论(0)
提交回复
热议问题