Jump to page: 1 2
Thread overview
why is assert() so useless?
Mar 17, 2004
imr1984
Mar 17, 2004
Vathix
Mar 17, 2004
Vathix
Mar 17, 2004
Matthew
Mar 17, 2004
Ant
Mar 17, 2004
imr1984
Mar 18, 2004
Juan C
Mar 18, 2004
Matthew
Mar 18, 2004
J C Calvarese
Mar 18, 2004
imr1984
Mar 18, 2004
Derek Parnell
Mar 19, 2004
Walter
Mar 20, 2004
imr1984
Mar 20, 2004
C. Sauls
Mar 20, 2004
Andrew Edwards
March 17, 2004
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 :-[


March 17, 2004
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
March 17, 2004
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 :-[
>
>


March 17, 2004
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

March 17, 2004
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");

:)


March 17, 2004
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
March 18, 2004
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
>


March 18, 2004
"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
> >
>
>


March 18, 2004
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.
March 18, 2004
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.


« First   ‹ Prev
1 2