assert(condition[, message]) patch

Derek Parnell derek at psych.ward
Wed May 31 18:13:13 PDT 2006


On Wed, 31 May 2006 17:05:16 -0700, kris wrote:

> Sean Kelly wrote:
> [snip]
>> There's a popular argument connected to contract programming which says 
>> that it makes no sense to test with contracts enabled and disable them 
>> in the release code.  I'd prefer not to take sides here, but I do think 
>> that in instances where the application should halt because a condition 
>> has not been met, an assert should be used.  If the programmer would 
>> prefer that the user not see file and line information, he can easily 
>> catch the error in main, report it however he wants to, and then return.
>> 
>> That said, my real goal here is merely to make assert a bit more 
>> flexible so users aren't inclined to roll their own error handling 
>> routines simply because they want to provide context information. 
> 
> Amen.
> 
> I find it surprising that the dmd support code still makes it awkward to 
> add file & line number info to all exceptions. Asserts are no less vague 
> in this respect. One has to wonder if this is deliberate or not?
> 
> On the other hand, Ares has provided for optional Exception file & line 
> ctor arguments for at least a year.
> 
> Seems quite clear that people *want* more flexible assert and exception 
> reporting. It also seems clear that everyone will roll their own if it's 
> not provided as basic functionality. I certainly would like to add it to 
> library code, since I suspect most folk would appreciate having it there :)
> 
> Is there a contrary argument or position? I mean, is there some negative 
> aspect of enabling (optional) better error reporting?

I suspect that this is the real issue. Namely that it is possible to
provide additional information at runtime when an 'assert' fires, but to do
so is awkward in comparison to a simple "assert( <Expression> )" statement.

One could always do something like ...

// ---- file: massert.d ----
private import std.string;
void myassert(bool res, char[] f, long l, char[] msg)
{
    if (!res)
    {
        throw new Exception(std.string.format("ASSERT in File: %s(%s), %s",
             f, l, msg));
    }
}


// --- file: myapp.d -----
import massert;
import std.string;
void main(char[][] pArgs)
{
   myassert( pArgs.length >= 2, __FILE__, __LINE__,
             "Not enough args supplied" );
   myassert( pArgs[1].length >= 2, __FILE__, __LINE__,
        std.string.format("Arg1 '%s' too short", pArgs[1])
        );
}


but I concede that this is not pretty.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
1/06/2006 10:55:52 AM



More information about the Digitalmars-d mailing list