On Saturday, 24 August 2013 at 00:45:46 UTC, H. S. Teoh wrote:
I've written up a proposal to solve the partially-constructed
object
problem[*] in D in a very nice way by extending scope guards:
http://wiki.dlang.org/DIP44
[*] The partially-constructed object problem is when you have a
class
(or struct) that must acquire some number of external
resources, usually
to set them as member fields, but before the ctor is able to
initialize
all of these fields, an Exception is thrown. Now the object is
in a
partially-constructed state: some member fields have been
initialized,
and need to be destructed in order to release the associated
external
resources, but other fields are still uninitialized so should
not be
destructed. This leads to the problem of, how do we clean up in
this
situation? We can't call the dtor -- the dtor assumes *all*
fields have
been set and will wrongly try to release resources that haven't
been
acquired yet. But we can't ignore the issue either -- the
resources that
*have* been required need to be released somehow. This DIP
proposes a
nice solution to this problem that fits in very well with the
existing
scope guards in D.
Destroy! ;-)
T
I like the feature. I wouldn't say this is the most important
thing to add here, the same can be achieved with scope(failure)
and destructor.