Against enforce()

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 17 08:24:12 PDT 2011


On Wed, 16 Mar 2011 21:07:54 -0400, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:

> On 03/16/2011 06:45 PM, bearophile wrote:
>> - It kills inlining (with the current DMD, and I don't think this  
>> problem will be fixed soon);
>
> Not a problem of enforce.
>

Why can't enforce be this:

T enforce(T, string file = __FILE__, int line = __LINE__)(T value, /* lazy
-- BUG: disables inlining */ const(char)[] msg = null);

Until the compiler is fixed?  I bet it would perform better even though
the message may be eagerly constructed.  Simply because in 99% of cases
(I've checked) the message is a string literal.

Essentially we keep saying D is on par with C for performance, and then
the standard library is riddled with un-optimizable code making that claim
patently false.  It doesn't help to say, "well yeah, I know it's not *now*
but trust me, we know what the problem is and we'll fix it in the next
1-60 months."

Given that this problem has been noted and existed for over a year, and
doesn't show any sign of being fixed, we should work around it until it is
fixed.

enforce should be pure, since it should be equivalent to if(!x) throw new
Exception(msg).  However, we cannot have a "function" that is the
equivalent.  This would all be possible with macros.  The workaround is
simple, replace instances of enforce in functions you want to be pure with
the equivalent if (!x) throw ...

All the other points, I disagree with bearophile, enforce is not assert
and should not be nothrow.

-Steve


More information about the Digitalmars-d mailing list