www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dynamic object inherane

May be this will sound strange. How about dymanic inhearence. I mean warapper
classes:

class A { // base class
	int x=0;
	int fn() { return x++; }
}

class B : A { // mutation 1
	int fn() { return x+3; }
}

class WA : A { // dynamic mutation (wrapper class)
	A parentA; // or =new A(); as default base class
	this(A a) { parentA=a; }
	~this() { delete parentA; parentA=null; }

	// a lot of hand writing wrappers (that can build automatic)
	int x() { return parentA->x; }
	int x(int v) { return parentA->x=v; }
	int fn() { return parentA->fn(); }
        // so on
}

class WA2 : *A { // the same but in brief form
	this(A a) { parentA=a; }
}

class WA3 : WA2 { // next class generation
	int fn() { return super.fn()-1; } 
}
//...

void tst() {
	WA1 w1=new WA2(new B);
	w1.fn();
	w1.parentA=new A(); // change class behavior runtime
	w1.fn();
	
	WA2 w2=new WA2(new B);
	w2.fn();
	w2.parentA=new A(); // change class behavior runtime
	w2.fn();
}

Some kind of agregations. May such contruction be practicaly usefull? What do
you think?
Mar 07 2008