Why assert is in the language?

Jonathan M Davis jmdavisProg at gmail.com
Tue Jun 22 15:18:48 PDT 2010

Steven Schveighoffer wrote:

> On Tue, 22 Jun 2010 17:07:02 -0400, Tomek Sowiński <just at ask.me> wrote:
>> Yes, why? It could be implemented in object.d in a similar fashion as
>> std.contracts.enforce. Does it do anything special that a library
>> function couldn't?
> all calls to assert are removed by the compiler in release mode.  I don't
> think there's a way to implement that via a library (it would be nice
> though!)
> -Steve

Also IIRC, the compiler uses assert(0) to ensure that functions blow up at 
runtime if you manage to hit the end of them without a return statement.

And it's not like it hurts anything to make it part of the language. Also, 
having them as part of the language gives the compiler better control of 
them and allows it to know more about them in order to treat them in a 
special manner, which it could not do with library functions.

All in all, there isn't really a downside (as far as I'm aware of anyway) to 
having them in the language itelf, and it helps the compiler deal with them.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list