Domanda

On fpcomplete.com, I was reading a tutorial on using yesod forms: https://www.fpcomplete.com/school/advanced-haskell/building-a-file-hosting-service-in-yesod/part%202?show=tutorials. Based on the tutorial, my main function is

main = do
titems <- atomically $ newTVar []
liftIO $ warpEnv (Links titems)

What I would like to do is start a Sql database in memory, and then put all the data from the forms in memory. But even a very simple example does not work:

main = runSqlite ":memory:" $ do
--  runMigration migrateAll
--  michaelId <- insert $ Person "Michael" $ Just 26
--  michael <- get michaelId
--  liftIO $ print michael
titems <- atomically $ newTVar []
liftIO $ warpEnv (Links titems)

The error I get is

src/Main.hs@39:13-39:36Couldn't match expected type `SqlPersistT
                                (Control.Monad.Logger.NoLoggingT
                                   (Control.Monad.Trans.Resource.ResourceT m0))
                                t0'
            with actual type `IO (TVar [a0])'
In a stmt of a 'do' block: titems <- atomically $ newTVar []
In the second argument of `($)', namely
  `do { titems <- atomically $ newTVar [];
        liftIO $ warpEnv (Links titems) }'
In the expression:
  runSqlite ":memory:"
  $ do { titems <- atomically $ newTVar [];
         liftIO $ warpEnv (Links titems) }

I understand that somehow the types are mismatched, but not sure what function I would have to use in order to fix it. I'd greatly appreciate it if someone could explain to me what is going on.

È stato utile?

Soluzione

You need to add liftIO in front of atomically, to lift the IO action into the Persistent monad.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top