在我观看了 George Wilson 的精彩演讲后(Next Level MTL https://github.com/gwils/next-level-mtl-with-classy-optics/blob/master/Slides.pdf)我尝试创建使用 MTL 的应用程序风格设计并决定使用仆人,看起来这个库不适合这种设计。
下面的代码无法编译,因为我无法将m转换为 Handler。
getItems :: (MonadIO m, MonadReader r m, HasNetworkConfig r) => m [Item]
getItems =
return [Item "foo" "bar"]
mkApp :: Application
mkApp = serve itemApi getItems
你可以在这里找到完整的例子:https ://github.com/paweln1986/ServantMTLStackOverflowExample
是否可以将任何单子与仆人一起使用?如何做到这一点?我尝试使用提升服务器但没有成功。你知道我在这里想念什么吗?
编译错误:
• No instance for (MonadReader r0 Handler)
arising from a use of ‘getItems’
• In the second argument of ‘serve’, namely ‘getItems’
In the expression: serve itemApi getItems
In an equation for ‘mkApp’: mkApp = serve itemApi getItems
|
40 | mkApp = serve itemApi getItems
| ^^^^^^^^
更短的例子:
type ReaderAPI = "ep1" :> Get '[JSON] Int :<|> "ep2" :> Get '[JSON] String :<|> Raw :<|> EmptyAPI
readerApi = Proxy :: Proxy ReaderAPI
readerServer :: (MonadIO m, HasNetworkConfig r, MonadReader r m) => ServerT ReaderAPI (AppT m)
readerServer = return 1797 :<|> view (networkConfig . host) :<|> Tagged (error "raw server") :<|> emptyServer
nt x = return undefined
mainServer = hoistServer readerApi nt readerServer :: Server ReaderAPI
这给了我下面的编译错误
• Ambiguous type variable ‘m0’ arising from a use of ‘readerServer’
prevents the constraint ‘(MonadIO m0)’ from being solved.
Probable fix: use a type annotation to specify what ‘m0’ should be.
These potential instances exist:
instance [safe] MonadIO IO -- Defined in ‘Control.Monad.IO.Class’
instance MonadIO m => MonadIO (AppT m) -- Defined in ‘Types’
instance MonadIO Handler
-- Defined in ‘Servant.Server.Internal.Handler’
...plus 18 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the third argument of ‘hoistServer’, namely ‘readerServer’
In the expression:
hoistServer readerApi nt readerServer :: Server ReaderAPI
In an equation for ‘mainServer’:
mainServer
= hoistServer readerApi nt readerServer :: Server ReaderAPI
|
64 | mainServer = hoistServer readerApi nt readerServer :: Server ReaderAPI
| ^^^^^^^^^^^^