Existence check for objects

Georg Wrede georg at nospam.org
Wed Jun 13 12:30:57 PDT 2007


Extrawurst wrote:
> OF schrieb:
>> Georg Wrede Wrote:
>>> Jarrett Billingsley wrote:
>>>> "Ary Manzana" wrote

>>>> I don't think the compiler should rewrite it, but I don't think it 
>>>> should be allowed either.  Either make it a warning or an error to 
>>>> ==/!= to null. 
>>>
>>> Count me in.
>>
>> Count me in too. I've had problems with this. At least a warning would 
>> be nice.
> 
> definitely ! i ran into this very often and i know many D beginners who 
> do. a warning cant hurt

Actually, why do we use the foo !is null idiom at all? (Or try to use 
the foo != null idiom?)

The following works as expected:



import std.stdio;

void main()
{
         Object o = new Object;

         // while(o !is null)
         while(o)
         {
                 writefln("Yee.");
                 o = null;
         }

         Object o2;

         while(!o2)
         {
                 writefln("Haw.");
                 o2 = new Object;
         }
}


The expected output being:

Yee.
Haw.


Embarrassingly, I don't seem to remember if there was a historical 
reason for not (at some time?) using "while(o)".

Anyhow, this would seem to be the canonical way to do it in a C family 
language in the first place. So maybe we should (of course first mark 
o==null as an error, and then) amend the documentation to suggest o 
itself as the Politically Correct test for nullness.

Finally, the *error message* should suggest o itself for the test.



More information about the Digitalmars-d mailing list