D2 full closure?

BCS ao at pathlink.com
Tue Jul 15 13:02:26 PDT 2008


Reply to Walter,

> BCS wrote:
> 
>> Reply to Walter,
>> 
>>> Frank Benoit wrote:
>>> 
>>>> Is this output expected?
>>>> 
>>> No, it's a bug. The output should be 0,1,2. Is it on bugzilla?
>>> 
>> What?!! The value for the delegates are copied at the point of the
>> '&'?
>> 
> No, they are never copied. The delegates should refer directly to the
> variable which, in the case of a closure, gets allocated on the heap
> rather than the stack frame.
> 

Oh crud, I read it wrong.

OTOH that problem is even worse than the issue I was thinking of:

is this valid? does it generate a different heap frame for each time through 
the loop?

alias void delegate() Runner;

void main(){
  Runner[] runner;
  int i;
  for(i = 0; i < 3; i++ ){
    const int ci = i;
    runners ~= delegate(){ writefln( "run with ci=%d, i=%d", ci, i ); }
  }
  foreach( runner; runners ){
    runner();
  }
}


is this valid?

void main()
{
  Runner a;
  {
      int i = 5;
      a = {i++;}
  }

  {
      int j = 6;
      a();
      assert(j==6);
  }
}





More information about the Digitalmars-d mailing list