A better assert() [was: Re: std.unittests [updated] for review]

Jonathan M Davis jmdavisProg at gmx.com
Fri Feb 4 04:17:15 PST 2011


On Friday 04 February 2011 04:10:37 bearophile wrote:
> Andrei:
> > We need to have at least a definite decision by Feb 7 from Don and
> > Walter that work will be put or not into improving assert as discussed.
> 
> I think that fixing assert() is a better strategy, even it may require more
> work. A problem with assert is that it wants to do too many things, so
> there are problems: - assert(0); to mean HALT, that gives some problems
> - assert(class_instance); that calls its invariant. (I vaguely remember a
> problem with this with struct instance invariants). - Not good enough for
> unittests, as seen in this thread.
> 
> So an effort to improve assert may solve all four those problems. Removing
> the two purposes (moving them to specialized and explicit features,
> specialized little tools) and adding the functionality we desire in
> unittesting.

Making assert smarter with regards to errors so that it's better suited for unit 
tests makes sense, and I do find the assert(class_instance) bit to be rather 
disturbing given that it conflicts with how conditional checks using class 
instances usually works (I only recently found out that assert worked that way 
with class instances). However, I don't see why there would be any problem with 
assert(0) meaning halt. It's a normal assert in non-release mode and it sticks 
around in release mode, becoming a halt instruction. I use it that way all the 
time. I don't see any problem with it whatsoever.

- Jonathan M Davis


More information about the Digitalmars-d mailing list