assert(false) in release == splinter in eye

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Oct 11 17:37:04 PDT 2010


On 10/10/2010 10:52 AM, Christopher Bergqvist wrote:
> 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?
>
> / Chris

I followed this thread. assert(false) is clearly an unpleasant 
irregularity in the language. In the beginning I was really bothered by 
it but with time I have to admit it's not a big deal in practice.

assert(false) could be in an ideal world replaced by an intrinsic called 
e.g. halt() that looks and feels like a regular function but is 
recognized by the compiler. No new keyword would be needed. But I don't 
think that would mark a significant improvement in the language.


Andrei


More information about the Digitalmars-d mailing list