www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Comparing to null?

reply Ben Phillips <Ben_member pathlink.com> writes:
if (someObject == null)
..

The above code compiles, but in my opinion it shouldn't. Naturally the correct
syntax for is "if (someObject is null)", but this is an easy mistake for
beginners to forget. It also doesn't help that the code generates an access
violation. Since code such as the above is always a bug then I think the
compiler should just explicitly check for improper comparisons to "null"
Jan 05 2006
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Ben Phillips wrote:

 if (someObject == null)
 ..
 
 The above code compiles, but in my opinion it shouldn't. Naturally the correct
 syntax for is "if (someObject is null)", but this is an easy mistake for
 beginners to forget. It also doesn't help that the code generates an access
 violation. Since code such as the above is always a bug then I think the
 compiler should just explicitly check for improper comparisons to "null"

Yes, this could definitely fall under a "you probably didn't mean to"... Like writing "if (x = 1)", which already gives a friendly warning* in D? * '=' does not give a boolean result --anders PS. After going through a lot of D code changing "if (someObject !== null)", to "if (someObject !is null)" I just gave up and wrote "if (someObject)" Besides being shorter (and much less ugly than !is), it also avoids the problem above. And it's not like D is going to get boolean comps anyway?
Jan 06 2006