www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - assert(object) fails to adhere to the principle of least surprise

reply Bernard Helyer <b.helyer gmail.com> writes:
If I do

    if (object) {
        ...
    }

What happens is fairly obvious, and is equivalent to

    if (object !is null) {
    }

However, if I do

    auto object = new Object();
    assert(object);

What I expect to happen is

    assert(object !is null);

Just as in the above example. What happens however is the program seg 
faults. Why? Because it turns out what DMD turns it (silently) into is

    object.checkInvariants();  // Whatever it's called.

This is bad enough, however it gets pants-on-head stupid as *object is 
not checked for null*. I think the silent rewrite is bad design, but not 
checking for null is so stupid, so obvious to anyone who actually uses 
the language, I can't believe it's existed for so long. The fact that

    assert(object);

and

    import std.exception;
    enforce(object);

do different things boggles my mind. One must write

   assert(object !is null);

or

   assert(!!object);

and every day it's like a giant stabbing pain. A stupid wrong headed 
design that makes my experience with D _worse_. Just expose a method for 
checking the invariant explicitly, and don't do this silent rewrite 
bullshit. Any chance of getting a change of behaviour?

FWIW, GDC doesn't do the rewrite, and SDC (the compiler I'm working on 
github.com/bhelyer/sdc) won't either. 
Jan 29 2011
next sibling parent reply Bernard Helyer <b.helyer gmail.com> writes:
A few corrections.

On Sat, 29 Jan 2011 12:02:57 +0000, Bernard Helyer wrote:
     auto object = new Object();
     assert(object);

This segfaults if object is null, which is obviously impossible in this example.
 
 FWIW, GDC doesn't do the rewrite, 

On structs, it does the rewrite on class instances (boo!).
Jan 29 2011
parent Bernard Helyer <b.helyer gmail.com> writes:
Further correction.

On Sat, 29 Jan 2011 12:09:41 +0000, Bernard Helyer wrote:
 FWIW, GDC doesn't do the rewrite,

On structs, it does the rewrite on class instances (boo!).

It checks for null then checks the invariant. I think that's a good compromise, given that an object without an invariant passes the assert.
Jan 29 2011
prev sibling parent reply Tomek =?ISO-8859-2?Q?Sowi=F1ski?= <just ask.me> writes:
Bernard Helyer napisa=B3:

 If I do
=20
     if (object) {
         ...
     }
=20
 What happens is fairly obvious, and is equivalent to
=20
     if (object !is null) {
     }
=20
 However, if I do
=20
     auto object =3D new Object();
     assert(object);
=20
 What I expect to happen is
=20
     assert(object !is null);
=20
 Just as in the above example. What happens however is the program seg=20
 faults. Why? Because it turns out what DMD turns it (silently) into is
=20
     object.checkInvariants();  // Whatever it's called.
=20
 This is bad enough, however it gets pants-on-head stupid as *object is=20
 not checked for null*. I think the silent rewrite is bad design, but not=

 checking for null is so stupid, so obvious to anyone who actually uses=20
 the language, I can't believe it's existed for so long. The fact that
=20
     assert(object);
=20
 and
=20
     import std.exception;
     enforce(object);
=20
 do different things boggles my mind. One must write
=20
    assert(object !is null);
=20
 or
=20
    assert(!!object);
=20
 and every day it's like a giant stabbing pain. A stupid wrong headed=20
 design that makes my experience with D _worse_. Just expose a method for=

 checking the invariant explicitly, and don't do this silent rewrite=20
 bullshit. Any chance of getting a change of behaviour?
=20
 FWIW, GDC doesn't do the rewrite, and SDC (the compiler I'm working on=20
 github.com/bhelyer/sdc) won't either.=20

http://d.puremagic.com/issues/show_bug.cgi?id=3D796 Vote up ;) --=20 Tomek
Jan 29 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 1/29/11, Tomek Sowi=F1ski <just ask.me> wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=3D796

 Vote up ;)

Wow, that is an old-school bug (2007!). :p I wonder which bug report is the oldest one that is still opened.
Jan 29 2011
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:

 On 1/29/11, Tomek Sowi=C5=84ski <just ask.me> wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=3D796

 Vote up ;)

Wow, that is an old-school bug (2007!). :p I wonder which bug report is the oldest one that is still opened.

That would be #107: Wrong filename in error message when using a mixin http://d.puremagic.com/issues/show_bug.cgi?id=3D107 -- = Simen
Jan 29 2011