www.digitalmars.com         C & C++   DMDScript  

D - Return Codes from Constructor

reply Chris Hobbs <Chris_member pathlink.com> writes:
I am unfamiliar with D but have read most of the user manual.

I am disappointed to find that constructors cannot return an error code since I
assume that they also cannot throw an exception (although I can't find this
stated anywhere).

In a telecommunications system, where the system cannot stop, it is essential
for a constructor to be able to return a code to say "I didn't initialise
properly, don't use me". This may result from the object being unable to get
enough memory or other resources. Without a return code or exception (as in C++)
it is necessary to define a public method

bool createdOK();

which the creator must remember to call to see whether the object is in a
zombiue state, having not really been created. This leads to all sorts of errors
when people forget to call createdOK();

Chris Hobbs
Apr 20 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
"Chris Hobbs" <Chris_member pathlink.com> wrote in message
news:c642il$okl$1 digitaldaemon.com...
 I am unfamiliar with D but have read most of the user manual.

 I am disappointed to find that constructors cannot return an error code
since I
 assume that they also cannot throw an exception (although I can't find
this
 stated anywhere).
They can.
 In a telecommunications system, where the system cannot stop, it is
essential
 for a constructor to be able to return a code to say "I didn't initialise
 properly, don't use me". This may result from the object being unable to
get
 enough memory or other resources. Without a return code or exception (as
in C++)
 it is necessary to define a public method

 bool createdOK();

 which the creator must remember to call to see whether the object is in a
 zombiue state, having not really been created. This leads to all sorts of
errors
 when people forget to call createdOK();

 Chris Hobbs
Apr 20 2004