Thread overview
Class Default Value
Apr 25, 2006
Alberto Simon
Apr 25, 2006
Derek Parnell
April 25, 2006
I have a question... When I declare a class object no matter what the definition is and set it to null I get an runtime exception whenever I try to compare that object to null... Is that correct behavior?? and if that is correct, how do I check for an uninitialized class in a fast and efficient manner?

Regards,
Alberto Simon
April 25, 2006
"Alberto Simon" <Alberto_member@pathlink.com> wrote in message news:e2k4f5$1efa$1@digitaldaemon.com...
>I have a question... When I declare a class object no matter what the definition
> is and set it to null I get an runtime exception whenever I try to compare
> that
> object to null... Is that correct behavior?? and if that is correct, how
> do I
> check for an uninitialized class in a fast and efficient manner?

Use "is", not "==".


April 25, 2006
On Tue, 25 Apr 2006 13:19:02 +1000, Alberto Simon <Alberto_member@pathlink.com> wrote:

> I have a question... When I declare a class object no matter what the definition
> is and set it to null I get an runtime exception whenever I try to compare that
> object to null... Is that correct behavior?? and if that is correct, how do I
> check for an uninitialized class in a fast and efficient manner?


When one codes ...
   class Foo {};
   Foo foo = new Foo;
   ...
   if (foo == null)

the compile converts this to call the implied class member function 'opEquals' so this code is equivalent to

  if (foo.opEquals(null))

and for that to work, 'foo' must be a valid class instance.

The way to check for an uninitialized class in D is to let the compiler know that's what you are trying to do, and the syntax for that is ...

   if (foo is null)

To test if it is initialized use

   if(foo !is null)
or
   if (! (foo is null) )

-- 
Derek Parnell
Melbourne, Australia