Assert

Georg Wrede georg at nospam.org
Sat Jun 30 14:18:04 PDT 2007


David B. Held wrote:
> BCS wrote:
>> Reply to David,
>>
>>> I think assert() will get fixed when we get AST macros.

Yes, it looks that way.

>>> 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.

"the builtin will only get called by Phobos" sounds a bit ominous.

>>> 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. ;>

Hmm. Unfortunately I agree.

>> 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. 

DAVID, please, take note: enforce might not be the optimum word for it. 
I'd rather suggest "ensure". (Yes, David and not Andrei, because I 
assume English is David's mother tongue, so he's got some influence on 
Andrei about this.)

IMHO, "enforce" is like holding somebody at gunpoint, while "ensure" is 
like an "ultimate check". In other words, "enforce" ends up (in the real 
world) in coercing, while "ensure" ends up in "double check this, and if 
it doesn't work, ask your boss".

> 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().

Yes. And if nothing else, this should convince Walter to not have 
assert() to be inconsistent.



More information about the Digitalmars-d mailing list