我想嵌入ReaderT
到另一个monad变压器。我该怎么做呢?下面的例子使用Scotty
,但我认为它会与任何其他monad相同。如何让ReaderT与另一个monad变压器一起工作?
{-# LANGUAGE OverloadedStrings #-}
import qualified Web.Scotty
import Web.Scotty.Trans
import Data.Text.Lazy
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Reader
import Control.Monad.Trans
data Config = Config Text
main :: IO()
main = do
let config = Config "Hello World"
-- how to I make this line work?
scottyT 3000 id id routes
routes :: ScottyT Text (ReaderT Config IO)()
routes = do
get "/" info
info :: ActionT Text (ReaderT Config IO)()
info = do
-- this part seems like it works!
Config message <- lift ask
text $ "Info: " `append` message
上线scottyT 3000 id id routes
这样的错误,因为scottyT
期望一个ScottyT Text IO()
。我如何完成这项工作?以下是当前的错误:
Server.hs: line 24, column 24:
Couldn't match type `ReaderT Config IO' with `IO'
Expected type: ScottyT Text IO()
Actual type: ScottyT Text (ReaderT Config IO)()
嗯,这个工程,但我不知道它是正确的:'flip runReaderT config $ scottyT 3000 id(flip runReaderT config)routes'。我不认为这是正确的考虑'runReaderT'需要两次,但我不知道为什么scotty知道为什么'scottyT'函数需要这个参数 – bheklilr 2014-09-26 18:14:08
你能否改变“Transformes stack”的顺序,即' ReaderT配置(ScottyT文本IO)()'? – chaosmasttter 2014-09-26 18:16:30
基于[this](http://stackoverflow.com/a/23190718/839246)的答案,你可以做'scottyT 3000(flip runReaderT)(flip runReaderT)路线,而这似乎是其他人的方式完成了。 – bheklilr 2014-09-26 18:19:49