www.digitalmars.com         C & C++   DMDScript  

D - why is assert() so useless?

reply imr1984 <imr1984_member pathlink.com> writes:
the messages that assert throw are so undescriptive, that they are quite
useless. I need them to tell me the file and line number the assert number was
thrown, otherwise i have to track through my large project looking for the
particular assert that threw up on me :-[
Mar 17 2004
next sibling parent reply Vathix <vathix dprogramming.com> writes:
imr1984 wrote:
 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert number was
 thrown, otherwise i have to track through my large project looking for the
 particular assert that threw up on me :-[
 
 
What I do is use my own Assert object that returns a useful string from toString() until the actual Assert does it. You should be able to just paste this into your source and start getting meaningful messages: class Assert : Object { private: char[] errstr; this(char[] filename, uint linnum) { errstr = "Assertion Failure " ~ filename ~ "(" ~ std.string.toString(linnum) ~ ")"; } public: /*************************************** * If nobody catches the Assert, this winds up * getting called by the startup code. */ void print() { puts(toString()); } char[] toString() { return errstr; } } -- Christopher E. Miller
Mar 17 2004
parent Vathix <vathix dprogramming.com> writes:
Vathix wrote:
 imr1984 wrote:
 
 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert 
 number was
 thrown, otherwise i have to track through my large project looking for 
 the
 particular assert that threw up on me :-[
What I do is use my own Assert object that returns a useful string from toString() until the actual Assert does it. You should be able to just paste this into your source and start getting meaningful messages: class Assert : Object { private: char[] errstr; this(char[] filename, uint linnum) { errstr = "Assertion Failure " ~ filename ~ "(" ~ std.string.toString(linnum) ~ ")"; } public: /*************************************** * If nobody catches the Assert, this winds up * getting called by the startup code. */ void print() { puts(toString()); } char[] toString() { return errstr; } }
Actually, you have to put this in there too: extern (C) static void _d_assert(char[] filename, uint line) { throw new Assert(filename, line); } so it finds your _d_assert() first and throws your Assert rather than the other. -- Christopher E. Miller
Mar 17 2004
prev sibling next sibling parent "Matthew" <matthew stlsoft.org> writes:
Agreed. This is something that requires attention soon.

"imr1984" <imr1984_member pathlink.com> wrote in message
news:c39l55$1rgg$1 digitaldaemon.com...
 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert number
was
 thrown, otherwise i have to track through my large project looking for the
 particular assert that threw up on me :-[
Mar 17 2004
prev sibling next sibling parent reply Ant <duitoolkit yahoo.ca> writes:
On Wed, 17 Mar 2004 13:51:33 +0000, imr1984 wrote:

 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert number was
 thrown, otherwise i have to track through my large project looking for the
 particular assert that threw up on me :-[
??? You mean the fatal errors. the assert actually does that (file and line) (dmd 0.81 linux) I started to do: try { ... } catch ( SomeError e ) { assert(0); } Ant
Mar 17 2004
next sibling parent imr1984 <imr1984_member pathlink.com> writes:
In article <pan.2004.03.17.14.53.25.668012 yahoo.ca>, Ant says...
On Wed, 17 Mar 2004 13:51:33 +0000, imr1984 wrote:

 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert number was
 thrown, otherwise i have to track through my large project looking for the
 particular assert that threw up on me :-[
??? You mean the fatal errors. the assert actually does that (file and line) (dmd 0.81 linux) I started to do: try { ... } catch ( SomeError e ) { assert(0); } Ant
i mean that asserts dont descibe where they threw from, so you have to find it out yourself, which almost defeats the point of them - it means you just have to do something like: throw new Error("there should be an assertion failure on line 13 but instead here is a lovely message instead"); :)
Mar 17 2004
prev sibling parent reply Juan C <Juan_member pathlink.com> writes:
assert should not be built into the language, each developer should be able to
easily define own.

On the other hand I never use assert anyway.

In article <pan.2004.03.17.14.53.25.668012 yahoo.ca>, Ant says...
On Wed, 17 Mar 2004 13:51:33 +0000, imr1984 wrote:

 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert number was
 thrown, otherwise i have to track through my large project looking for the
 particular assert that threw up on me :-[
??? You mean the fatal errors. the assert actually does that (file and line) (dmd 0.81 linux) I started to do: try { ... } catch ( SomeError e ) { assert(0); } Ant
Mar 17 2004
next sibling parent "Matthew" <matthew stlsoft.org> writes:
"Juan C" <Juan_member pathlink.com> wrote in message
news:c3b1sh$1ad7$1 digitaldaemon.com...
 assert should not be built into the language, each developer should be
able to
 easily define own.

 On the other hand I never use assert anyway.
How do you use the DBC features if you don't use assert? Do you throw exceptions? Matthew P.S. If you don't use the DBC features, then I strongly recommend you wait for a while, go and rejig your code and fill it with ins, outs and invariants, and post a response saying something like "oh, exception for contracts, of course. ;) ". Otherwise, you've just scarred away most of your potential users. <g>
 In article <pan.2004.03.17.14.53.25.668012 yahoo.ca>, Ant says...
On Wed, 17 Mar 2004 13:51:33 +0000, imr1984 wrote:

 the messages that assert throw are so undescriptive, that they are
quite
 useless. I need them to tell me the file and line number the assert
number was
 thrown, otherwise i have to track through my large project looking for
the
 particular assert that threw up on me :-[
??? You mean the fatal errors. the assert actually does that (file and line) (dmd 0.81 linux) I started to do: try { ... } catch ( SomeError e ) { assert(0); } Ant
Mar 18 2004
prev sibling parent J C Calvarese <jcc7 cox.net> writes:
Juan C wrote:
 assert should not be built into the language, each developer should be able to
 easily define own.
 
 On the other hand I never use assert anyway.
If you don't use it, then why do you care whether it's built into the language or not? I think assert could be improved, but I'd like it to still be built into the language. (I do use assert though not as often as I should.)
 In article <pan.2004.03.17.14.53.25.668012 yahoo.ca>, Ant says...
 
On Wed, 17 Mar 2004 13:51:33 +0000, imr1984 wrote:
[...] -- Justin http://jcc_7.tripod.com/d/
Mar 18 2004
prev sibling next sibling parent reply Carlos Santander B. <Carlos_member pathlink.com> writes:
In article <c39l55$1rgg$1 digitaldaemon.com>, imr1984 says...
the messages that assert throw are so undescriptive, that they are quite
useless. I need them to tell me the file and line number the assert number was
thrown, otherwise i have to track through my large project looking for the
particular assert that threw up on me :-[
That's weird. Try this: interface A {} class B : A {} void foo (A a) { assert ( (cast(B) a) !== null ) ; } void main() { foo(new B); } Using DMD 0.81 on Win98 I get: "Error: AssertError Failure inter.d(3)". ------------------- Carlos Santander B.
Mar 18 2004
parent reply imr1984 <imr1984_member pathlink.com> writes:
hmm strange, what was the command line for dmd?

In article <c3cs9k$1brk$1 digitaldaemon.com>, Carlos Santander B. says...
In article <c39l55$1rgg$1 digitaldaemon.com>, imr1984 says...
the messages that assert throw are so undescriptive, that they are quite
useless. I need them to tell me the file and line number the assert number was
thrown, otherwise i have to track through my large project looking for the
particular assert that threw up on me :-[
That's weird. Try this: interface A {} class B : A {} void foo (A a) { assert ( (cast(B) a) !== null ) ; } void main() { foo(new B); } Using DMD 0.81 on Win98 I get: "Error: AssertError Failure inter.d(3)". ------------------- Carlos Santander B.
Mar 18 2004
parent reply Carlos Santander B. <Carlos_member pathlink.com> writes:
In article <c3cub9$1fbg$1 digitaldaemon.com>, imr1984 says...
hmm strange, what was the command line for dmd?

In article <c3cs9k$1brk$1 digitaldaemon.com>, Carlos Santander B. says...
That's weird. Try this:

interface A {}
class B : A {}
void foo (A a) { assert ( (cast(B) a) !== null ) ; }
void main() { foo(new B); }

Using DMD 0.81 on Win98 I get: "Error: AssertError Failure inter.d(3)".

-------------------
Carlos Santander B.
dmd inter Where the contents of inter.d are those 4 lines above. ------------------- Carlos Santander B.
Mar 18 2004
parent "Derek Parnell" <Derek.Parnell psyc.ward> writes:
On Thu, 18 Mar 2004 19:58:35 +0000 (UTC) (19/Mar/04 06:58:35 AM)
, Carlos Santander B. <Carlos_member pathlink.com> wrote:

 In article <c3cub9$1fbg$1 digitaldaemon.com>, imr1984 says...
 hmm strange, what was the command line for dmd?

 In article <c3cs9k$1brk$1 digitaldaemon.com>, Carlos Santander B.  
 says...
 That's weird. Try this:

 interface A {}
 class B : A {}
 void foo (A a) { assert ( (cast(B) a) !== null ) ; }
 void main() { foo(new B); }

 Using DMD 0.81 on Win98 I get: "Error: AssertError Failure inter.d(3)".

 -------------------
 Carlos Santander B.
dmd inter Where the contents of inter.d are those 4 lines above. ------------------- Carlos Santander B.
Confirmed. C:\dparnell>type inter.d interface A {} class B : A {} void foo (A a) { assert ( (cast(B) a) !== null ) ; } void main() { foo(new B); } C:\dparnell>dmd inter.d C:\DPARNELL\DMD\BIN\..\..\dm\bin\link.exe inter,,,user32+kernel32/noi; C:\dparnell>inter Error: AssertError Failure inter.d(3) C:\dparnell> -- Derek
Mar 18 2004
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"imr1984" <imr1984_member pathlink.com> wrote in message
news:c39l55$1rgg$1 digitaldaemon.com...
 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert number
was
 thrown, otherwise i have to track through my large project looking for the
 particular assert that threw up on me :-[
It already does: ----------------------------------- C:\cbx\mars>type test.d void main() { assert(0); } C:\cbx\mars>dmd test \dm\bin\link test,,,user32+kernel32/noi; C:\cbx\mars>test Error: AssertError Failure test.d(4) C:\cbx\mars> ------------------------------------------
Mar 18 2004
parent reply imr1984 <imr1984_member pathlink.com> writes:
well it doesnt for me :(
Im using DMD 0.81 on XP

In article <c3dgv8$2gfr$1 digitaldaemon.com>, Walter says...
"imr1984" <imr1984_member pathlink.com> wrote in message
news:c39l55$1rgg$1 digitaldaemon.com...
 the messages that assert throw are so undescriptive, that they are quite
 useless. I need them to tell me the file and line number the assert number
was
 thrown, otherwise i have to track through my large project looking for the
 particular assert that threw up on me :-[
It already does: ----------------------------------- C:\cbx\mars>type test.d void main() { assert(0); } C:\cbx\mars>dmd test \dm\bin\link test,,,user32+kernel32/noi; C:\cbx\mars>test Error: AssertError Failure test.d(4) C:\cbx\mars> ------------------------------------------
Mar 20 2004
next sibling parent "C. Sauls" <ibisbasenji yahoo.com> writes:
Works for me, DMD 0.81 on WinXP(Home)...

-C. Sauls
-Invironz

imr1984 wrote:
 well it doesnt for me :(
 Im using DMD 0.81 on XP
Mar 20 2004
prev sibling parent Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
On Sat, 20 Mar 2004 12:52:22 +0000 (UTC), imr1984 
<imr1984_member pathlink.com> wrote:

 well it doesnt for me :(
 Im using DMD 0.81 on XP
That's too bad...Maybe something wrong with the way you set up your system. I'm using DMD 0.81 on XP and it works just fine. F:\>ver Microsoft Windows XP [Version 5.1.2600] F:\>ren inpchar.d test.d F:\>type test.d void main() { assert(0); } F:\>dmd test F:\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi; F:\>test Error: AssertError Failure test.d(3) F:\>
Mar 20 2004