![](/img/trans.png)
[英]Haskell IO: Couldn't match expected type `IO a0' with actual type
[英]Haskell error: couldn't match expected type ‘ServerPartT IO a0’ with actual type '[Response]'
當我嘗試編譯代碼時,會發生兩個錯誤。
第一個是:
Couldn't match expected type ‘ServerPartT IO a0’
with actual type ‘[Response]’
In a stmt of a 'do' block:
msum
(map (\ (a, b) -> dir a b)
$ routes
staticDir
redirectUrlGraphEmail
redirectUrlGraphPost
aboutContents
privacyContents)
++
[do { nullDir;
seeOther "graph" (toResponse "Redirecting to /graph") },
notFoundResponse]
In the second argument of ‘($)’, namely
‘do { decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096);
msum
(map (\ (a, b) -> dir a b)
$ routes
staticDir
redirectUrlGraphEmail
redirectUrlGraphPost
aboutContents
privacyContents)
++
[do { nullDir;
.... },
notFoundResponse] }’
In a stmt of a 'do' block:
simpleHTTP serverConf
$ do { decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096);
msum
(map (\ (a, b) -> dir a b)
$ routes
staticDir
redirectUrlGraphEmail
redirectUrlGraphPost
aboutContents
privacyContents)
++
[do { nullDir;
.... },
notFoundResponse] }
它提到了三個代碼塊,其中一個
第二個是:
Couldn't match type ‘ServerPartT IO’ with ‘[]’
Expected type: [[Response]]
Actual type: [ServerPartT IO Response]
In the first argument of ‘msum’, namely
‘(map (\ (a, b) -> dir a b)
$ routes
staticDir
redirectUrlGraphEmail
redirectUrlGraphPost
aboutContents
privacyContents)’
In the first argument of ‘(++)’, namely
‘msum
(map (\ (a, b) -> dir a b)
$ routes
staticDir
redirectUrlGraphEmail
redirectUrlGraphPost
aboutContents
privacyContents)’
In a stmt of a 'do' block:
msum
(map (\ (a, b) -> dir a b)
$ routes
staticDir
redirectUrlGraphEmail
redirectUrlGraphPost
aboutContents
privacyContents)
++
[do { nullDir;
seeOther "graph" (toResponse "Redirecting to /graph") },
notFoundResponse]
我也不太確定錯誤的位置。
似乎這兩個錯誤的含義完全相反。 我現在很困惑。 有人可以幫忙解釋一下嗎? 謝謝!
原始代碼在這里:
runServer :: IO ()
runServer = do
configureLogger
staticDir <- getStaticDir
redirectUrlGraphEmail <- retrieveAuthURL testUrl
redirectUrlGraphPost <- retrieveAuthURL testPostUrl
aboutContents <- LazyIO.readFile $ markdownPath ++ "README.md"
privacyContents <- LazyIO.readFile $ markdownPath ++ "PRIVACY.md"
-- Start the HTTP server
simpleHTTP serverConf $ do
decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096)
msum
(map (\ (a, b) -> dir a b) $ routes staticDir redirectUrlGraphEmail redirectUrlGraphPost aboutContents privacyContents ) ++
[ do
nullDir
seeOther "graph" (toResponse "Redirecting to /graph"),
notFoundResponse
]
routes
在另一個模塊中的位置:
routes :: [Char] -> T.Text -> T.Text -> Text -> Text -> [ (String, ServerPart Response)]
routes staticDir redirectUrlGraphEmail redirectUrlGraphPost aboutContents privacyContents = [
("grid", gridResponse),
("graph", graphResponse),
("image", graphImageResponse),
...
]
我想問題是您的do塊中的第二條語句是msum (...) ++ [...]
,被解析為(msum [...]) ++ [...]
。 因此,編譯器會看到++
並推斷這是列表monad中的一條語句。 但是根據其余代碼,do塊應使用ServerPartT IO
monad。 這就是為什么您收到第一條錯誤消息,指出ServerPartT IO a0
和'[Response]
不匹配。
要解決此問題 ,請嘗試添加更多的括號:
msum ((...) ++ [...])
或其他美元運營商:
msum $ (...) ++ [...]
第二條錯誤消息是相同問題的另一個結果。 由於編譯器推斷該語句在列表monad中,因此它假定msum
屬於列表monad。 因此,msum的參數應該是list monad中的一個動作列表(=列表列表),但是它是ServerPartT IO
monad中的一個動作列表。 我希望當您添加缺少的括號時該錯誤會消失,並且編譯器會看到msum
應該來自ServerPartT IO
monad。
錯誤位置應在復制部件上方的編譯器輸出中提供。 應該有一個文件名,然后是冒號,然后是行號。 編譯器還會報告與錯誤相關的源代碼。 請注意,例如,第一個錯誤消息沒有三個不相關的源代碼段,但是第一段是第二段的一部分,第二段是第三段的一部分。 因此,編譯器首先向您顯示有錯誤的片段,然后再為越來越大的片段提供更多的上下文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.