DIP44: scope(class) and scope(struct)

Artur Skawina art.08.09 at gmail.com
Sat Aug 24 04:01:12 PDT 2013


On 08/24/13 08:30, Piotr Szturmaj wrote:
> 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

> 2. allow referring to local variables and create the closure. This causes additional memory allocation and also reference to the closure must be stored somewhere, perhaps in the class hidden field. Of course, this is a "no go", I'm writing this here for comparison.

That is what he's actually proposing. And, yes, it's not a good idea.
Implementing it via runtime delegates, implicit captures/allocs and 
extra hidden fields inside aggregates would work, but the cost is too
high. It's also not much of an improvement over manually registering
and calling the delegates. Defining what happens when a ctor fails
would be a good idea, having a cleanup method which defaults to `~this`,
but can be overridden could help too.

There are other problems with that DIP, like making it harder to see
what's actually going on, by splitting the dtor code and having it
interleaved with another separate flow.


It *is* possible to implement a similar solution without any RT cost,
but it would need:
a) flow analysis - to figure out the cleanup order, which might not be 
    statically known (these cases have to be disallowed)
b) a different approach for specifying the cleanup code, so that 
    implicit capturing of ctor state doesn't happen and it's not
    necessary to read the complete body of every ctor just to find
    out what a dtor does.


artur


More information about the Digitalmars-d mailing list