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

Reply via email to