我正在尝试在使用 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敏锐的眼睛。
最佳答案
您已将 main 声明为 () 类型,而不是 IO () 类型。
关于haskell - 为什么 GHC 对于 `IO b` 的应用程序推断类型为 `hSetBuffering` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2349922/