Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `void main` in D

Tags:

d

I've seen D code that uses void main. Is this legal? If yes, is returning non-void (int) also legal? Why is this allowed in the language?

like image 256
Paul Manta Avatar asked Nov 06 '11 09:11

Paul Manta


1 Answers

From the D Language Reference

main() Func­tion

For con­sole pro­grams, main() serves as the entry point. It gets called after all the mod­ule ini­tial­iz­ers are run, and after any unittests are run. After it re­turns, all the mod­ule destruc­tors are run. main() must be de­clared using one of the fol­low­ing forms:

void main() { ... }
void main(char[][] args) { ... }
int main() { ... }
int main(char[][] args) { ... }

So void main is legal.

From the same docs, return statement part:

A re­turn exits the cur­rent func­tion and sup­plies its re­turn value. Ex­pres­sion is re­quired if the func­tion spec­i­fies a re­turn type that is not void. The Ex­pres­sion is im­plic­itly con­verted to the func­tion re­turn type

So it appears that returning something from a void function is not explicitly disallowed (and indeed doing so compiles fine).

(Why would you want to do that though?)

like image 63
Mat Avatar answered Oct 05 '22 10:10

Mat