Thread overview
Request: add message parameter to assert()
Jun 08, 2005
Sean Kelly
Jun 10, 2005
Matthew
Jun 13, 2005
Ben Hinkle
June 08, 2005
The old horse still has a bit of life in it!

If we can assume that the current assert expression to evaluate as:

# void assert( bit eval );

Is there any chance this could be changed to:

# void assert( bit eval, char[] msg = null );

Since the default behavior of assert in D is to throw an exception, it seems quite reasonable that the programmer be given a means to supply the message that exception carries.  For DMD, this would change:

# extern (C) void _d_assert( char[] file, uint line );

to:

# extern (C) void _d_assert( char[] file, uint line, char[] msg = null );

I'd do this myself except assert is treated as a keyword in D, and it seems unreasonable to ask users to call a different function if they want to supply a message.


Sean


June 10, 2005
gets my vote

"Sean Kelly" <sean@f4.ca> wrote in message news:d87tsv$2sd9$1@digitaldaemon.com...
> The old horse still has a bit of life in it!
>
> If we can assume that the current assert expression to evaluate as:
>
> # void assert( bit eval );
>
> Is there any chance this could be changed to:
>
> # void assert( bit eval, char[] msg = null );
>
> Since the default behavior of assert in D is to throw an exception, it seems quite reasonable that the programmer be given a means to supply the message that exception carries.  For DMD, this would change:
>
> # extern (C) void _d_assert( char[] file, uint line );
>
> to:
>
> # extern (C) void _d_assert( char[] file, uint line, char[] msg = null );
>
> I'd do this myself except assert is treated as a keyword in D, and it seems unreasonable to ask users to call a different function if they want to supply a message.
>
>
> Sean
>
> 


June 11, 2005
If I do so recall, I posted about why I still think this is necessary, despite the response that file and line numbers should be enough for developers (an argument that obviously ignored open source software.)

-[Unknown]


> The old horse still has a bit of life in it!
> 
> If we can assume that the current assert expression to evaluate as:
> 
> # void assert( bit eval );
> 
> Is there any chance this could be changed to:
> 
> # void assert( bit eval, char[] msg = null );
> 
> Since the default behavior of assert in D is to throw an exception, it seems
> quite reasonable that the programmer be given a means to supply the message that
> exception carries.  For DMD, this would change:
> 
> # extern (C) void _d_assert( char[] file, uint line );
> 
> to:
> 
> # extern (C) void _d_assert( char[] file, uint line, char[] msg = null );
> 
> I'd do this myself except assert is treated as a keyword in D, and it seems
> unreasonable to ask users to call a different function if they want to supply a
> message.
> 
> 
> Sean
> 
> 
June 13, 2005
"Sean Kelly" <sean@f4.ca> wrote in message news:d87tsv$2sd9$1@digitaldaemon.com...
> The old horse still has a bit of life in it!
>
> If we can assume that the current assert expression to evaluate as:
>
> # void assert( bit eval );
>
> Is there any chance this could be changed to:
>
> # void assert( bit eval, char[] msg = null );
>
> Since the default behavior of assert in D is to throw an exception, it
> seems
> quite reasonable that the programmer be given a means to supply the
> message that
> exception carries.  For DMD, this would change:
>
> # extern (C) void _d_assert( char[] file, uint line );
>
> to:
>
> # extern (C) void _d_assert( char[] file, uint line, char[] msg = null );
>
> I'd do this myself except assert is treated as a keyword in D, and it
> seems
> unreasonable to ask users to call a different function if they want to
> supply a
> message.
>
>
> Sean

Once Exceptions get stack trace abilities the file/lineno will be less
crucial (though the linno will still let you tell which assert in a given
function failed). If one doesn't mind writing a bit more for message asserts
here's a simple solution
private import std.format;
void assertStr(int val,...) {
    char[] s;
    void putc(dchar c){ std.utf.encode(s, c); }
    if (!val) {
        // I would call std.string.sformat but it takes ... so
        // I can't :-(   Someone should add va_list versions.
        std.format.doFormat(&putc, _arguments, _argptr);
        throw new Exception(s);
    }

}
int main() {
    int a,b;
    a = 10;
    version(Assert) assertStr(a == b, "oops, b was %d",b);
    version(Assert) assertStr(a == b, "%s(%d) another",__FILE__,__LINE__);
    return 0;
}