如何避免在使用Syntastic时出现“‘main’ is not defined in module ‘Main’”错误

28

我正在尝试用Haskell编写一个模块。它没有main函数,因为它不是一个独立的程序。

我刚开始使用syntastic,但它一直在报告:

The IO action ‘main’ is not defined in module ‘Main’

这会阻止它报告我模块中的其他真正错误。如果我尝试通过添加虚拟主程序来解决这个问题,它会开始抱怨其他所有东西都是"Defined but not used"。

我该如何告诉Syntastic(以及其底层使用的任何检查器),不应该有一个main


7
在开头加上 module Foo where - ErikR
2个回答

31

使用 ghc-mod 进行测试(这是 syntastic 的后端,但也可以使用 hdevtools):

$ cat test1.hs
f :: Int -> Int
f x = x + 1
$ ghc-mod check test.hs
test1.hs:1:1:The IO action 'main' is not defined in module 'Main'
$ cat test2.hs
module Test where

f :: Int -> Int
f x = x + 1
$ ghc-mod check test.hs
$

所以,您只需要在文件中添加一个虚拟模块声明,警告就会消失。

出现此警告的原因是因为在Haskell中,默认情况下任何未声明的模块都具有名称Main。由于您没有定义 main :: IO() 且模块名称隐式为Main,所以ghc-mod会抛出警告。如果将其声明为其他名称,则ghc-mod认为您只是从模块中导出了所有内容,因为一切都隐式地从模块导出。


9

虽然回答比较晚,但为了完整性,加入以下内容可以停止警告。

main :: IO ()
main = return ()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接