【发布时间】:2011-01-21 22:30:10
【问题描述】:
我正在尝试在使用 GHC 6.10 的 Haskell 程序中使用 hSetBuffering。
当我尝试这个非常简单的程序时:
module Ctlc
where
import IO
main :: ()
main = do hSetBuffering stdout NoBuffering
return ()
我收到一条令人费解的错误消息:
ctlc.hs:8:10:
Couldn't match expected type `()' against inferred type `IO b'
In a stmt of a 'do' expression: hSetBuffering stdout NoBuffering
In the expression:
do hSetBuffering stdout NoBuffering
return ()
In the definition of `main':
main = do hSetBuffering stdout NoBuffering
return ()
我不明白为什么 GHC 推断出 IO b 的类型,因为 ghci 声称
Prelude Data.List IO> :t hSetBuffering
hSetBuffering :: Handle -> BufferMode -> IO ()
回答:我愚蠢地在main 上输入了错误的类型。感谢ja 的敏锐眼睛。
【问题讨论】:
-
我确实想知道为什么 GHC 不会更早地失败:
()不是 monad,所以do { ... } :: ()是不可能的,即使hSetBuffering stdout NoBuffering :: ()。如果错误是“can't match () against IO () in main = do ...”,您可能已经注意到缺少的IO。