haskell - 为什么 GHC 对于 `IO b` 的应用程序推断类型为 `hSetBuffering` ?

标签 haskell typeerror

我正在尝试在使用 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/

相关文章:

haskell - 根据输出功能设定输入的上限

Haskell - Parsec::解析空格直到字符串文字

javascript - 如果匹配则获取第一个值

python - 在 Keras 模型中加载时出现 TypeError

haskell - 检测什么函数引发了异常 Prelude。!! : negative index

haskell - 为什么这种模式连接而不是返回纯数组?

haskell - 为什么这个最小的 Gtk2Hs 程序会泄漏内存

python - TypeError: 'float' 对象不可下标 --Python

TensorFlow - "TypeError: Fetch Argument None"

node.js - bull-arena 要求将队列构造函数提供给 Arena