Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 813
Khuldraeseth na'Barya Avatar asked May 10 '26 22:05

Khuldraeseth na'Barya


1 Answers

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?

like image 130
Li-yao Xia Avatar answered May 14 '26 08:05

Li-yao Xia