DIP44: scope(class) and scope(struct)

Piotr Szturmaj bncrbme at jadamspam.pl
Fri Aug 23 23:30:49 PDT 2013


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
>
> Destroy! ;-)

I see some possible implementation problem. Using a scope guard in a 
single function is straightforward because code is executed in the same 
scope.

However, in your proposal scope guard is split to two different 
functions/scopes. This may require copying local stack variables and 
storing them somewhere if they're referred to in the scope(this).

For example:

ResourceManager resManager;

class C {
     Resource res;
     this(int resourceId) {
         res = resManager.acquireResource(resourceId);
         scope(this) resManager.releaseResource(resourceId);
     }
}

Here the resourceId value must be stored somewhere so it can be used 
later, when the object is destructed.

I'm thinking of two possible "solutions" to this problem:

1. disallow referring to local variables in scope(this). In this case 
resourceId should be stored in a field of class C or in the Resource 
itself (it may not always be allowed).

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.



More information about the Digitalmars-d mailing list