www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Request: add message parameter to assert()

reply Sean Kelly <sean f4.ca> writes:
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
Jun 08 2005
next sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
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

 

Jun 10 2005
prev sibling next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
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
 
 

Jun 11 2005
prev sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"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; }
Jun 13 2005