assert(false) in release == splinter in eye
Christopher Bergqvist
chris at digitalpoetry.se
Sun Oct 10 16:53:07 PDT 2010
Thanks for the support guys. :)
Unfortunately "halt" would still need to be a keyword if one wants to keep
the full behavior of assert(0), where the compiler knows that it affects the
control-flow of the program.
Legal:
int main()
{
assert(0);
}
Illegal (Error: function D main has no return statement, but is expected to
return a value of type int):
int main()
{
int a = 0;
assert(a);
}
2010/10/10 Tomek Sowiński <just at ask.me>
> Christopher Bergqvist napisał:
>
> > Hi,
> >
> > Time for some Sunday nitpicking. While reading TDPL, one thing that stuck
> > out to me was the special behavior of assert(false). Consider the
> > following program compiled with "-release".
> >
> > void main()
> > {
> > int a = 0;
> > assert(a);
> > }
> >
> > That program will run without errors. Changing the type of variable a
> from
> > "int" to "enum" results in the program segfaulting thanks to the compiler
> > being able to know the value of the expression "a" at compile time and
> > inserting a HLT/"halt" instruction. Having the ability to change
> something
> > subtle in a more complex expression or series of expressions without
> > realizing you made a compile time assert(false) which crashes your
> program
> > feels ugly.
> >
> > I would prefer it if assert() didn't have this special type of behavior,
> > and that a "halt" keyword or equivalent was introduced. What do you
> think?
>
> I have the same feeling. 'halt' is good, 'fail' is good too. It doesn't
> have to be a keyword, a
> function in object.d would suffice.
>
> BTW, does anybody know the reason for the assert(0) infernal syntax?
>
> --
> Tomek
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20101011/ae13ee43/attachment.html>
More information about the Digitalmars-d
mailing list