January 02, 2011 Re: void main returning int - why compiles? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred_Nowak | Manfred Nowak: > _and_ foo() is not marked to have no side effects. You are right, thank you for the quotation. So I have updated this, because it's not a bug: http://d.puremagic.com/issues/show_bug.cgi?id=5399 I'm sure there's a rationale behind that special case in the D specs. Is someone able to explain? It looks like an unsafe corner case. Bye, bearophile |
January 02, 2011 Re: void main returning int - why compiles? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred_Nowak | 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 |
Copyright © 1999-2021 by the D Language Foundation