assert(object) fails to adhere to the principle of least surprise

Tomek Sowiński just at ask.me
Sat Jan 29 05:30:21 PST 2011


Bernard Helyer napisał:

> 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. 

http://d.puremagic.com/issues/show_bug.cgi?id=796

Vote up ;)

-- 
Tomek



More information about the Digitalmars-d mailing list