在Haskell中写或不写`module Main where` [英] To write or not to write `module Main where` in Haskell
问题描述
main
应该驻留在名为Main的模块中。但是,即使你不写 main in的文件顶部的 module main其中
,源代码编译并且在使用GHC时看起来正确。
问题是:
- 编写
模块Main
并且不写它的区别是什么?
- 哪一个是首选?
>
解决方案 没有真正的区别, module Main(main)where
在你没有自己指定头时是隐式定义。从 Haskell 98报告:
模块的缩写形式(仅包含
模块体)是允许的。如果使用这种方法,那么头部被假定为
为 module Main(main),其中
。
我宁愿将一个显式定义转换为一个隐式定义,但对于一个 Main.hs
而言,这是一个次要偏好。
Haskell 98 specification says that the entry point of a program, namely, function main
, should reside in the module called Main, by convention. However, even if you don't write module Main where
at the top of the file you write main
in, the source code compiles and seems working correct when you're using GHC.
The question is:
- What's the difference between writing
module Main where
and not writing it?
- Which one is preferred?
解决方案 There isn't really a difference, module Main (main) where
would be the implicit definition when you don't specify a header yourself. From the Haskell 98 Report:
An abbreviated form of module, consisting only of
the module body, is permitted. If this is used, the header is assumed
to be module Main(main) where
.
I would prefer an explicit definition to an implicit one but, for a Main.hs
it's a minor preference.
这篇关于在Haskell中写或不写`module Main where`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文