alwaysAssert() [was: Against enforce()]

Walter Bright newshound2 at digitalmars.com
Thu Mar 17 17:16:39 PDT 2011


On 3/17/2011 4:38 PM, dsimcha wrote:
> On 3/17/2011 7:12 PM, Walter Bright wrote:
>>
>> Sure, but there is plenty wrong with using enforce() for bug detection
>> even if alwaysAssert does not exist. For one thing, such uses encourage
>> others to misunderstand and misuse enforce.
>>
>> Additionally, alwaysAssert is an obvious one liner. I think such things
>> need to be very frequently used to consider them part of the standard
>> library. Otherwise, we risk Phobos becoming a morass of trivia.
>
> What makes you think it wouldn't be used very frequently?

I don't know if it would or would not be used very frequently.


> It seems silly to me
> to turn off asserts in non-performance-critical bits of code just because I
> don't want bounds checking or the more expensive asserts.

The use case is more constrained than that. Because of the existence of:

     if (!condition) assert(0);

the alwaysAssert is constrained to those purposes where the user feels the need 
for file/line/message for bug detection in released code, and also does not want 
to use:

     if (!condition) { writeln("my message %s %s", __FILE__, __LINE__); assert(0); }

Furthermore, nothing prevents the user from writing his own alwaysAssert.

For inclusion in Phobos, the more trivial something is, the higher utility it 
needs to have to justify it. Of course, there's an element of subjectivity to 
these judgments. One liners, though, should always be subject to significant 
scrutiny and justification. Once in, we'll be stuck with them for a long time.


More information about the Digitalmars-d mailing list