DIP44: scope(class) and scope(struct)

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sat Aug 24 06:33:56 PDT 2013


On 24/08/13 02:44, 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

Converse problem: suppose that you need certain resources to be _created_ in 
order for the struct to work correctly.  Consider:

     struct Foo
     {
         Resource res;

         this(int n)
         {
             res = getResource(n);
         }
     }

     void main()
     {
         auto foo1 = Foo(10);  // Good! Resource is initialized
         Foo foo2;   // Bad! Resource is uninitialized
     }

... where we can probably reasonably assume that getResource is doing some kind 
of allocation.

Is there any way to handle this needed-at-entry-to-scope requirement, in a 
similar way to how your DIP handles exit of scope?


More information about the Digitalmars-d mailing list