Proper Use of Assert and Enforce

Chris Pons cmpons at gmail.com
Wed Mar 14 19:50:04 PDT 2012


Thank you for the valuable information! The difference between 
assert and enforce is now clearer in my mind.

Also, that's a great trick with enforce.

On Thursday, 15 March 2012 at 01:08:02 UTC, Jonathan M Davis 
wrote:
> On Wednesday, March 14, 2012 20:15:16 Spacen Jasset wrote:
>> Is enforce then a way of generating exceptions in an easier 
>> way rather
>> than using some sort of "if (failure) throw" syntax? In other 
>> words, I
>> assume it's a mechanism to help you use exceptions, and not 
>> some new
>> semantic.
>
> It is purely a way to make throwing an exception use a syntax 
> similar to
> assert and save a line of code.
>
> if(!condition)
>     throw new Exception(msg);
>
> becomes
>
> enforce(condition, msg);
>
> or
>
> enforce(condition, new Exception(msg));
>
> It arguably adds very little, but some people really like it.
>
> - Jonathan M Davis




More information about the Digitalmars-d-learn mailing list