www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - obj == null is an exception?

reply "Ben Harper" <ben gls.co.za> writes:
How does one test if an object is null?
obj == null throws an exception!
Surely this contravenes the norm quite significantly! 
May 13 2005
next sibling parent reply "Ben Harper" <ben gls.co.za> writes:
I expect the answer is going to be "use exceptions".
This makes me sad. I have been quite excited about D. But look at the 
following:

MsBaseWindow wnd = WindowTable.Get( hWnd );
if ( wnd != NULL )
  dosomething();
else
  dosomethingelse();


Now-- with exceptions I have to say
-----------------------------------------

try
{
  MsBaseWindow wnd = WindowTable.Get( hWnd );
  dosomething();
}
catch( object o )
{
  dosomethingelse();
}

1. The exception method is more code.
2. In the above method i know exactly what the problem is: There is no 
window associated with the hWnd.
3. In the exceptioned method anything could have gone wrong. It tells me 
less.


"Ben Harper" <ben gls.co.za> wrote in message 
news:d621cn$21pf$1 digitaldaemon.com...
 How does one test if an object is null?
 obj == null throws an exception!
 Surely this contravenes the norm quite significantly!
 

May 13 2005
next sibling parent reply Peter Mackay <a_pointy_stick.NoJunkMail yahoo.co.uk> writes:
There are a few more operators for this kind of thing, since == and != 
compare the objects, not the references.

	if (object !== null) // note double ==.
	if (object === null) // note triple ===.

or

	if (!(object is null))

etc

Peter

Ben Harper wrote:
 I expect the answer is going to be "use exceptions".
 This makes me sad. I have been quite excited about D. But look at the 
 following:
 
 MsBaseWindow wnd = WindowTable.Get( hWnd );
 if ( wnd != NULL )
   dosomething();
 else
   dosomethingelse();
 
 
 Now-- with exceptions I have to say
 -----------------------------------------
 
 try
 {
   MsBaseWindow wnd = WindowTable.Get( hWnd );
   dosomething();
 }
 catch( object o )
 {
   dosomethingelse();
 }
 
 1. The exception method is more code.
 2. In the above method i know exactly what the problem is: There is no 
 window associated with the hWnd.
 3. In the exceptioned method anything could have gone wrong. It tells me 
 less.
 
 
 "Ben Harper" <ben gls.co.za> wrote in message 
 news:d621cn$21pf$1 digitaldaemon.com...
 
How does one test if an object is null?
obj == null throws an exception!
Surely this contravenes the norm quite significantly!


May 13 2005
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
Peter Mackay wrote:

 There are a few more operators for this kind of thing, since == and !=
 compare the objects, not the references.
 
 if (object !== null) // note double ==.
 if (object === null) // note triple ===.

Note that this syntax has been removed from the docs. Lars Ivar Igesund
May 13 2005
parent Peter Mackay <a_pointy_stick.NoJunkMail yahoo.co.uk> writes:
 Note that this syntax has been removed from the docs.

Ah, !== and === are deprecated? If so, I'll update my code. Sorry for the dodgy advice Ben. Peter
May 13 2005
prev sibling parent Lars Ivar Igesund <larsivar igesund.net> writes:
Nope, D differentiates between equality and identity, see
http://www.digitalmars.com/d/expression.html
and scroll down to "Identity Expressions".

Basically you use != and == to compare values (and in your example below
that is the values of the references). To check for identity use 'is', like

if (obj is null) { ... }
else if (!(obj is null)) { ... }

Lars Ivar Igesund

Ben Harper wrote:

 I expect the answer is going to be "use exceptions".
 This makes me sad. I have been quite excited about D. But look at the
 following:
 
 MsBaseWindow wnd = WindowTable.Get( hWnd );
 if ( wnd != NULL )
   dosomething();
 else
   dosomethingelse();
 
 
 Now-- with exceptions I have to say
 -----------------------------------------
 
 try
 {
   MsBaseWindow wnd = WindowTable.Get( hWnd );
   dosomething();
 }
 catch( object o )
 {
   dosomethingelse();
 }
 
 1. The exception method is more code.
 2. In the above method i know exactly what the problem is: There is no
 window associated with the hWnd.
 3. In the exceptioned method anything could have gone wrong. It tells me
 less.
 
 
 "Ben Harper" <ben gls.co.za> wrote in message
 news:d621cn$21pf$1 digitaldaemon.com...
 How does one test if an object is null?
 obj == null throws an exception!
 Surely this contravenes the norm quite significantly!


May 13 2005
prev sibling parent "Ben Harper" <ben gls.co.za> writes:
Terribly sorry!
Something I saw earlier threw me into the assumption that classes were 
implicitly *.

Apologies!

"Ben Harper" <ben gls.co.za> wrote in message 
news:d621cn$21pf$1 digitaldaemon.com...
 How does one test if an object is null?
 obj == null throws an exception!
 Surely this contravenes the norm quite significantly!
 

May 13 2005