www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - null pointer return problem

reply ano <ano_member pathlink.com> writes:
i'm porting some code over to D from C++ and am now facing the NULL pointer
return problem. in C++ i have a class on which you can call a findChild()
function and it will either return the child or NULL if no child with the given
name exists. both cases are perfectly ok, e.g. imagine a INI config file that
has some required variables in it but also some additional variables that aren't
required to be present in the file. an application may want to check for those
variables and if it's not there then just make some default values or something.
in D i cannot make this work because of an access violation (see below).

-------------
Symbol foo = config.findChild ("foo"); // this is ok, "foo" exists
Symbol bar = config.findChild ("bar"); //error, "bar" doesn't actually exist
assert (bar != null); // access violation before the assert the even fires.
assert (null != bar); //same as above
if (bar != null) //same as above
-------------

i'm not really sure what is the best way to make this work.

) should i change the "Symbol" return value to a "Symbol*" - this should work
but pointers are ugly.

) on startup, allocate a special NULL_SYMBOL that is always there and is only
used to compare against.

) let findChild() throw a "new SymbolNotFoundException(name_of_symbol)".  i'm
not sure about this s it makes the calling code bloated.

which would you prefer or what else could be done. or am i missing something
obvious maybe...

Best Regards,
ano
Oct 08 2004
parent reply Derek Parnell <derek psych.ward> writes:
On Fri, 8 Oct 2004 08:57:43 +0000 (UTC), ano wrote:

 i'm porting some code over to D from C++ and am now facing the NULL pointer
 return problem. in C++ i have a class on which you can call a findChild()
 function and it will either return the child or NULL if no child with the given
 name exists. both cases are perfectly ok, e.g. imagine a INI config file that
 has some required variables in it but also some additional variables that
aren't
 required to be present in the file. an application may want to check for those
 variables and if it's not there then just make some default values or
something.
 in D i cannot make this work because of an access violation (see below).
 
 -------------
 Symbol foo = config.findChild ("foo"); // this is ok, "foo" exists
 Symbol bar = config.findChild ("bar"); //error, "bar" doesn't actually exist
 assert (bar != null); // access violation before the assert the even fires.
 assert (null != bar); //same as above
 if (bar != null) //same as above
 -------------
 
 i'm not really sure what is the best way to make this work.
There is a special way to do this is in D. Try this instead ... assert (bar !== null); assert (null !== bar); if (bar !== null) Notice that there are two (2) '=' symbols used '!=='. There is also a '===' (3 equals) to check for null. eg. "if (bar === null) " An alternate syntax is "if (!(bar is null)) " and "if (bar is null)" -- Derek Melbourne, Australia 8/10/2004 7:20:21 PM
Oct 08 2004
parent reply ano <ano_member pathlink.com> writes:
In article <ck5man$15ud$1 digitaldaemon.com>, Derek Parnell says...
There is a special way to do this is in D. Try this instead ...

 assert (bar !== null);
 assert (null !== bar);
 if (bar !== null)

Notice that there are two (2) '=' symbols used '!=='. There is also a '==='
(3 equals) to check for null. eg.  "if (bar === null) " 

An alternate syntax is "if (!(bar is null)) "  and "if (bar is null)"

-- 
Derek
Melbourne, Australia
8/10/2004 7:20:21 PM
thanks, Derek, it works fine now. i wasn't even aware of that syntax.
Oct 08 2004
parent Derek <derek psyc.ward> writes:
On Fri, 8 Oct 2004 11:03:19 +0000 (UTC), ano wrote:

 In article <ck5man$15ud$1 digitaldaemon.com>, Derek Parnell says...
There is a special way to do this is in D. Try this instead ...

 assert (bar !== null);
 assert (null !== bar);
 if (bar !== null)

Notice that there are two (2) '=' symbols used '!=='. There is also a '==='
(3 equals) to check for null. eg.  "if (bar === null) " 

An alternate syntax is "if (!(bar is null)) "  and "if (bar is null)"

-- 
Derek
Melbourne, Australia
8/10/2004 7:20:21 PM
thanks, Derek, it works fine now. i wasn't even aware of that syntax.
Its documented under the Expressions section, sub-section "Identity Expressions". I believe its required because the syntax "instance == expression" is really shorthand for "instance.opEquals(expression)" which means that before the opEquals() function can be called, "instance" must be a valid reference. Thus if "instance" is null, we get an access violation. This is why we need a different method to see if the reference is null before trying to use it. -- Derek Melbourne, Australia
Oct 08 2004