为什么强制执行 Maybe monad?
Why is a Maybe monad enforced?
我正在尝试使用 Network.Linklater 包实现一个基本的 slackbot:
https://github.com/hlian/linklater
这个包定义了以下函数:
slashSimple :: (Command -> IO Text) -> Application
slashSimple f =
slash (\command _ respond -> f command >>= (respond . responseOf status200))
我正在尝试这样消费:
kittensBot :: Command -> IO Text
kittensBot cmd = do
putStrLn("+ Incoming command: " ++ show cmd)
return "ok"
main :: IO ()
main = do
putStrLn ("Listening on port: " ++ show port)
run port (slashSimple kittensBot)
where
port = 3001
这会产生(在编译时):
Main.hs:20:28:
Couldn't match type ‘Maybe Command’ with ‘Command’
Expected type: Maybe Command -> IO Text
Actual type: Command -> IO Text
In the first argument of ‘slashSimple’, namely ‘kittensBot’
In the second argument of ‘run’, namely ‘(slashSimple kittensBot)’
然而slashSimple
的签名是(Command -> IO Text) -> Application
。 kittensBot
的签名不应该满足吗?为什么不呢?
虽然 GitHub master 上 slashSimple
的定义如您所报告的那样,但 linklater-3.2.0.0 中的 Hackage 版本是
slashSimple :: (Maybe Command -> IO Text) -> Application
如果您想在 Hackage 上使用该软件包,您需要将 kittensBot 更新为类似以下内容:
kittensBot :: Maybe Command -> IO Text
kittensBot Nothing = ...
kittensBot (Just cmd) = do
putStrLn("+ Incoming command: " ++ show cmd)
return "ok"
或者,您可以从 GitHub 下载软件包并手动安装。
我正在尝试使用 Network.Linklater 包实现一个基本的 slackbot:
https://github.com/hlian/linklater
这个包定义了以下函数:
slashSimple :: (Command -> IO Text) -> Application
slashSimple f =
slash (\command _ respond -> f command >>= (respond . responseOf status200))
我正在尝试这样消费:
kittensBot :: Command -> IO Text
kittensBot cmd = do
putStrLn("+ Incoming command: " ++ show cmd)
return "ok"
main :: IO ()
main = do
putStrLn ("Listening on port: " ++ show port)
run port (slashSimple kittensBot)
where
port = 3001
这会产生(在编译时):
Main.hs:20:28:
Couldn't match type ‘Maybe Command’ with ‘Command’
Expected type: Maybe Command -> IO Text
Actual type: Command -> IO Text
In the first argument of ‘slashSimple’, namely ‘kittensBot’
In the second argument of ‘run’, namely ‘(slashSimple kittensBot)’
然而slashSimple
的签名是(Command -> IO Text) -> Application
。 kittensBot
的签名不应该满足吗?为什么不呢?
虽然 GitHub master 上 slashSimple
的定义如您所报告的那样,但 linklater-3.2.0.0 中的 Hackage 版本是
slashSimple :: (Maybe Command -> IO Text) -> Application
如果您想在 Hackage 上使用该软件包,您需要将 kittensBot 更新为类似以下内容:
kittensBot :: Maybe Command -> IO Text
kittensBot Nothing = ...
kittensBot (Just cmd) = do
putStrLn("+ Incoming command: " ++ show cmd)
return "ok"
或者,您可以从 GitHub 下载软件包并手动安装。