Versioned std.exception.bailOut()?

bearophile bearophileHUGS at lycos.com
Fri Jun 15 05:17:31 PDT 2012


This is the std.typecons.Nullable.get() method:

@property ref inout(T) get() inout pure @safe
{
     enforce(!isNull);
     return _value;
}



And this is the std.exception.enforce() and bailOut():

T enforce(T, string file = __FILE__, size_t line = __LINE__)
     (T value, lazy const(char)[] msg = null) @safe pure
{
     if (!value) bailOut(file, line, msg);
     return value;
}

private void bailOut(string file, size_t line, in char[] msg) 
@safe pure
{
     throw new Exception(msg ? msg.idup : "Enforcement failed", 
file, line);
}



What do you think about replacing the bailOut() with:


version (halting_enforce)
{
     private void bailOut(string file, size_t line, in char[] msg) 
@safe pure nothrow
     {
         assert(0, msg ? msg : "Enforcement failed");
     }
}
else
{
     private void bailOut(string file, size_t line, in char[] msg) 
@safe pure
     {
         throw new Exception(msg ? msg.idup : "Enforcement 
failed", file, line);
     }
}


Even if this version is not documented in Phobos docs (because 
the inliner someday will be better), today it allows methods that 
must be fast like Nullable.get() to be inlined (but I have not 
tested this), regaining the lost performance.

Bye,
bearophile


More information about the Digitalmars-d mailing list