assert(condition[, message]) patch
Daniel Keep
daniel.keep.lists at gmail.com
Tue May 30 23:52:08 PDT 2006
BCS wrote:
> In article <e5im92$hlo$2 at digitaldaemon.com>, akcom says...
> [...]
>> Just my thoughts, I think it's definitely got some merits, although one
>> must consider that the assert( statementToTest ) would give you the line
>> that threw the assert, which makes a message a bit less important.
>
> if the message is non static then a lot of info can be printed.
>
> assert(str.length < 5, `the string "` ~ str ~ `" is more than 5 char long");
>
> I'll vote for this.
>
I like the idea, but here's a better one that I could have used when
testing my MMX routines:
# ubyte[] a = new ubyte[4096]; // src1
# ubyte[] b = new ubyte[4096]; // src2
# ubyte[] c = new ubyte[4096]; // dst
# ubyte[] d = new ubyte[4096]; // result calculated "long hand"
#
# // ...
#
# adds(a, b, c);
# assert(c == d);
Working out *why* it's asserted is no fun whatsoever. YES it's told you
there's a bug, but in this case, it hasn't even told you what that bug
is. Just that it exists.
In this case, using a message would help; I could tell myself where the
assert had failed. Even better would be a few more asserts:
# assertArrayEqual(c, d);
Which might print:
AssertError foo.d line 2001: mismatch at element 42: expected "6", got "9".
I would have KILLED for that a few weeks ago :) I'd implement that
myself, except that `assert` seems to be the ONLY statement in D that
automatically passes the source file and line number to the exception.
Walter: is there any technical reason why we can't get Exceptions to
automatically have source file and line number assigned when instantiated?
Hmm. Seem to have gone off topic somewhat. Gomen.
-- Daniel
--
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list