void main returning int - why compiles?
bearophile
bearophileHUGS at lycos.com
Sun Jan 2 04:08:27 PST 2011
Manfred Nowak:
> | Expression is allowed even if the function specifies a void return
> | type. The Expression will be evaluated, but nothing will be returned.
> | If the Expression has no side effects, and the return type is void,
> | then it is illegal.
> http://www.digitalmars.com/d/2.0/statement.html (cited 01/01/11)
>
> _and_ foo() is not marked to have no side effects.
Walter has closer my issue with this comment:
http://d.puremagic.com/issues/show_bug.cgi?id=5399
>It is not a dangerous corner case, it is a deliberate design choice. It is meant to facilitate writing generic code so that the same code can be generated for void and non-void return values.<
But I don't understand still, I don't buy Walter's explanation still. I write my comments in D.learn because I don't want to fill Bugzilla with too much discussions.
This compiles:
int bar() { return 0; }
void main() {
return bar();
}
While this doesn't, and this semantic difference looks like corner case of the general D rule (the good D rule seems that if it's a void function you can't return a value different from void, and if it's not a void function then you must return a value of the right type):
int bar() { return 0; }
void main() {
auto temp = bar();
return temp;
}
If I want to create a void function foo(), and I want to generate code inside it, can't I just avoid to add a "return" statement in the generated code, instead of adding return and then letting the compiler silently ignore an eventually present return value?
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list