extension to scope

Alex Burton alexibu.nospamplease at mac.com
Sun Jan 28 01:30:52 PST 2007


I would like to say that the ability to declare a class reference as scope, which guarantees that the destructor is called when exceptions occur, is a great feature that allows much more elegant code than putting try catches every where.

However I think that it is somewhat limited in that references to other objects held by the class are not guaranteed to be freed.

I would like to see the scope keyword's meaning extended so that any objects referenced by the scoped class are freed if no other references to them exist, and that this is done recursively.

This would allow the logical use of scope beyond simple little convienience classes to large transaction type objects which need to be rolled back when an exception occurs.

This is of great importance to me when I have classes representing a transaction that may use some real resources such as files, ports etc.

Using an example I would like the code below to output 
~C
~B
~A
finished

import std.stdio;

class A
{
	~this() { writefln("~A"); }		
};

class B
{
	A a;
	this() { a = new A(); }	
	~this() { writefln("~B"); }		
};

class C
{
	B b;
	this() { b = new B(); }
	~this() { writefln("~C"); }		
};

int main()
{
	{
		scope auto x = new C;
	}
	writefln("finished");
	return 0;
}



More information about the Digitalmars-d mailing list