Proper Use of Assert and Enforce
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 5 00:26:31 PST 2016
On 02/05/2016 12:01 AM, Suliman wrote:
>> 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);
>
>
> So enforce is just macros on top of:
> if(!condition)
> throw new Exception(msg);
>
> ?
Basically, yes (but of course no macros in D. :) ).
T enforce(E : Throwable = Exception, T)(T value, lazy const(char)[]
msg = null, string file = __FILE__, size_t line = __LINE__)
if (is(typeof({ if (!value) {} })))
{
if (!value) bailOut!E(file, line, msg);
return value;
}
https://github.com/D-Programming-Language/phobos/blob/master/std/exception.d#L360
Ali
More information about the Digitalmars-d-learn
mailing list