assert(condition[, message]) patch

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Fri Jun 2 01:52:03 PDT 2006


Daniel Keep wrote:
> 
> Bruno Medeiros wrote:
>> Daniel Keep wrote:
>>> 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?
>>>
>> You mean when thrown, no?
>>
>>> Hmm.  Seem to have gone off topic somewhat.  Gomen.
>>>
>>>     -- Daniel
>>>
>>
> 
> # try
> # {
> #     // ...
> # }
> # catch( Exception e )
> # {
> #     // Hmm... nevermind
> #     throw e;
> # }
> 
> See the problem? :P
> 

Easily solvable I think. 'throw' will only fill file-line info once for 
a given exception (that is, it fills only if the exception hasn't that 
info already).
But now that you mention that idiom, I wonder how other languages deal 
with that. I'll have to check it out

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list