Returning a delegate, strange behaviour: D limitation?

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Dec 2 11:29:06 PST 2006


I don't think this is a bug, just a limitation due to D's lack of lexical
closures. However, I'm still confused as to why this happens. See comments in
example code:
--
class Foo {
	void delegate() dg() {
		return () {
			// just g(): asserts (like it should)
			// just h(1): asserts
			// just i(1,1): Access Violation
			
			// both g() and h(1): doesn't assert when called from f(), otherwise Access
Violation
			// both g() and i(1,1): doesn't assert when called from f(), otherwise Access
Violation
			// both h(1) and i(1,1): Access Violation
			
			// all three: doesn't assert when called from f(), otherwise Access Violation
			
			// am I getting random behaviour due to overstepping the bounds of where
delegates can reach?
			// the context pointer shouldn't matter, it should only be using the this
pointer, right?
			
			g();
			h(1);
			i(1,1);
		};
	}
	
	void f() { dg()(); }
	
	void g()             { assert (false); }
	void h(int x)        { assert (false); }
	void i(int x, int y) { assert (false); }
}

void main() {
	Foo foo = new Foo();
	foo.f();
	// foo.dg()();
}
--
If there's a clear and legitimate reason for this not working, is it worth
filing a bug about how DMD can't detect it at compile time?

Of course, what I'm most interested in is if there's any way to get something
like this to work <g>. This bit me when trying to make a function which would
take an array, and return a delegate usable as a foreach aggregate to loop over
that array.



More information about the Digitalmars-d-learn mailing list