Can a member function return a delegate to itself?

BCS BCS at pathlink.com
Wed May 23 10:03:09 PDT 2007


Steve Teale wrote:
> Something like:
> 
> class Foo
> {
>     int a;
> 
>     this() { a = 0; }
> 
>     void delegate(int) sum(int n) 
>               { a += n; return cast(void delegate(int)) &this.sum; }
> }

as mentioned, the reason this isn't directly doable is that the type is 
undefinable. However this is strictly a limitation of the D Syntax and 
their is no problem with actually doing it if you can tell DMD how. I 
have does this using typing tricks before:


struct S
{
	S delegate(int,int) dg;
}

struct O
{
	int k;
	S go(int i, int j)
	{
		O* o = new O
		o.k = k+i+j;
		S ret;
		ret.dg = &o.go;
		return ret;
	}
}



More information about the Digitalmars-d mailing list