Throwing specific Error Obejct for asserts

monarch_dodra monarchdodra at gmail.com
Thu Jan 10 08:11:00 PST 2013


On Thursday, 10 January 2013 at 15:54:21 UTC, Andrei Alexandrescu 
wrote:
> On 1/10/13 6:23 AM, monarch_dodra wrote:
>> So question: Why don't we have, just like for enforce, the 
>> possibility
>> of simply writing:
>> //----
>> assert(i <= j, new RangeError());
>> //----
>
> Define another function...?
>
> Andrei

Well, I would. I'd write an overload, but I can't, because assert 
is built-in.

I'd have to provide a new name (such as assertError). This would 
not be as convenient as having an overload. That, and a library 
function can't match assert's built-in functionality. For example:

//----
struct S()
{
   version(assert)
     bool isValid = false; //debug only variable

   void foo()
   {
     assertError(isValid, new RangeError()); //HERE
   }
}
//----

The problem is that at best, assertError can be a 
noop-implementation in release, but the call is still there. The 
first argument will still get evaluated. Further more, it may not 
even compile...


More information about the Digitalmars-d mailing list