Returning a delegate, strange behaviour: D limitation?

BCS BCS at pathlink.com
Mon Dec 4 10:18:02 PST 2006


Deewiant wrote:
> 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:
> --
[...]
> --
> 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.


I'd have to dig around a bit more to be sure but I think it amounts to 
the delegate that is returned includes a pointer to the stack frame of 
the function in which it is declared. After that function returns, this 
is an invalid pointer, so things go wonky. The solution would be to do 
something like this:

class Foo {
  void delegate() dg() {
   struct R{
    Foo from;
     dg() {
     from.g();
     from.h(1);
     from.i(1,1);
    }
   }
   auto ret = new R;
   ret.from = this;
   return &ret.dg;
  }
}

I have ended up needing to do this often enough to wish for something 
like this:


class Foo {
  void delegate() dg() {
   return this.() {  // use the provided pointer as context
    from.g();
    from.h(1);
    from.i(1,1);
   }
  }
}

another version that just came to mind would use with

class Foo {
  void delegate() dg() {
   return delegate() with(this){// use the provided pointer as context
    from.g();
    from.h(1);
    from.i(1,1);
   }
  }
}



More information about the Digitalmars-d-learn mailing list