digitalmars.D - obj == null is an exception?
- Ben Harper (3/3) May 13 2005 How does one test if an object is null?
- Ben Harper (26/30) May 13 2005 I expect the answer is going to be "use exceptions".
- Peter Mackay (9/50) May 13 2005 There are a few more operators for this kind of thing, since == and !=
- Lars Ivar Igesund (3/8) May 13 2005 Note that this syntax has been removed from the docs.
- Peter Mackay (3/4) May 13 2005 Ah, !== and === are deprecated? If so, I'll update my code. Sorry for
- Lars Ivar Igesund (9/46) May 13 2005 Nope, D differentiates between equality and identity, see
- Ben Harper (6/10) May 13 2005 Terribly sorry!
How does one test if an object is null? obj == null throws an exception! Surely this contravenes the norm quite significantly!
May 13 2005
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
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
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
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
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
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









Peter Mackay <a_pointy_stick.NoJunkMail yahoo.co.uk> 