Search code examples
haskellexit-code

Why is main's return not an exit code?


C and C++ allow us to return an exit code from main. Why does Haskell not do the same? Seems to me that it would be simple enough; just require main :: IO Int rather than IO t.

I realize that the following will not behave as a C programmer might expect:

main :: IO Int
main = do
    return 1                   -- Execution continues, thanks to (>>)
    putStrLn "Unreachable?"
    return 2                   -- Exit code 2?

This sort of exit code might be tricky to do right, but how tricky really? Seems nicer to me than having to import System.Exit.


Solution

  • It's not worth it. 99% of Haskell programs would just do return 0, that's pure boilerplate. It adds an extra very specific channel for errors, when exceptions (which System.Exit leverages) already do just as fine a job. How often do you actually need to import System.Exit for fine-grained return codes?