I learned from chapter 9 of Learn You A Haskell For Great Good that
By convention, we don't usually specify a type declaration for
main
.
As far as I can tell, this convention is widespread. However, if I compile, using the -Wall
flag, a program that lacks a type signature for main
, such as
-- test.hs
-- main :: IO ()
main = print (1 :: Int)
GHC does issue a warning:
$ ghc -Wall test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
test.hs:2:1: Warning:
Top-level binding with no type signature: main :: IO ()
Linking test ...
$
I'm confused... If a type signature for main
is indeed superfluous, why would -Wall
cause GHC to complain when it's missing? Are there good reasons (aside from getting rid of that warning) for specifying main
's type anyway?
The signature of the main method is public static void main (String [] ags). public static void main (String a []) is the main entry point signature for a typical Java program. So you should get on with this method signature. Java Runtime tries to find a method with name " main " with argument types "String []".
In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types and order of the arguments contained by a function.
14 types of signatures you must avoid. 1 #1. Strike-through signature. This is one type of signature I always find alarming. The signature sample below has been taken from the suicide note of ... 2 #2. Camouflage signature. 3 #3. Small signature. 4 #4. Trace-back signature. 5 #5. Scribbled signature. More items
Using a typed signature in your business is legal and accepted. But for it to be legally valid, you must adhere to the following rules: Prove that the signer wanted to sign by providing options like “Cancel.” Prove that the signer wanted to carry out their business electronically.
Well, generally speaking, as that warning makes clear, it's always a good idea to give top-level bindings a type signature. In fact, it would be more reasonable to say
By convention, we do specify a type declaration for everything1.
Certainly, in a big project, main
itself makes up a neglectable effort, so it really doesn't make any sense to omit the signature. Just write it out, for sake of consistency.
However, though Haskell is great for properly structured projects and actually there's a tendency to write almost everything in libraries, it's also surprisingly good as a quick scripting language, for stuff other people would write in Python or Perl. And in those cases, you generally don't care that much about safety and good documentation etc., you just want to quickly write down something as concise as possible that does the job. You also normally don't compile those scripts with -Wall
but just execute them with runhaskell
. And as scripts always need to contain a main
(unlike most other Haskell source files), it is indeed sensible enough to omit the signature here.
I'd still suspect that the majority of Haskellers nowadays do write main::IO()
even in the simplest scripts, if just out of habit.
1Only everything on the top-level, that is. Local signatures sometimes do make sense as well, but often they rather clutter the code.
It's actually a very good idea to write a type signature for main
, since otherwise if you get too fancy trying to write things in point-free form, you can end up with main
of type IO (IO ())
. This is accepted (the language standard says main
just has to have some type of the form IO a
) but the "inner IO action" that is the result of main
will just be discarded, which is almost certainly not what you wanted (you probably wanted to join
it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With