簡體   English   中英

為什么一個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下載程序包並手動安裝它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM