我想嵌入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