To write or not to write `module Main where` in Ha

2019-03-17 20:05发布

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:

  1. What's the difference between writing module Main where and not writing it?
  2. Which one is preferred?

1条回答
霸刀☆藐视天下
2楼-- · 2019-03-17 20:23

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.

查看更多
登录 后发表回答