How to "scope"?

Martin martinbbjerregaard at gmail.com
Mon Feb 3 09:30:30 PST 2014


Seeing as the scope keyword is (being?) deprecated, how would you 
handle something like this:

class Test
{
private:
   string str;

   this(string str)
   {
     this.str = str;
   }

public:
   static Test createFromString(string str)
   {
     return new Test();
   }

}

void main()
{

   // at the end of the scope, test is destroyed and memory is 
freed
   scope Test test = Test.createFromString("test");

}

The only solution I can think of is using scope(exit) something 
like this:
scope(exit) { destroy(test); GC.free(cast(void*)test); }

which seems clumsy. Any other, better solutions?



More information about the Digitalmars-d-learn mailing list