Assert
Sean Kelly
sean at f4.ca
Fri Jun 29 07:51:30 PDT 2007
David B. Held wrote:
> BCS wrote:
>> Reply to David,
>>
>>> I think assert() will get fixed when we get AST macros. It won't get
>>> fixed by Walter, of course, but rather, by us. Everyone will invent
>>> their favorite assert(), and the builtin will only get called by
>>> Phobos.
>>> And then, 10 years down the road, Walter will be writing "The Design
>>> and Evolution of D", where he will have to explain why assert(o)
>>> doesn't
>>> do the obvious thing, even though everyone else's does. ;>
>>
>> what is there to fix? (that is not a rhetorical question)
>
> Well, for one, the fact that "assert(obj)" doesn't do what 99% of noobs
> (including me!) expect it to do. Two, you can't write a custom assert()
> that has non-standard messaging. For instance, there is an extremely
> common idiom in all languages (that support exceptions) that basically
> looks like this:
>
> if (!everything_ok)
> {
> throw SomeException("Helpful message");
> }
>
> Andrei happens to call this micro-pattern "enforce()", which I think is
> a perfectly reasonable name for it. What we would like to do is
> something like so:
>
> #define enforce(cond, message) \
> if (!cond) throw SomeException(#cond + ": " + message);
>
> Bar* ptr = foo();
> enforce(ptr != null, "foo() failed");
>
> Obviously there's a thousand ways to riff on that basic idea, but try
> writing this function in D. As you can see, it's essentially a form of
> assert().
Seems like the main problem is that assert is compiled out in release
builds, correct? Throwing a custom message and such already works.
Sean
More information about the Digitalmars-d
mailing list