D2 full closure?

BCS ao at pathlink.com
Wed Jul 16 09:47:31 PDT 2008


Reply to Walter,

> BCS wrote:
> 
>> Oh crud, I read it wrong.
>> 
>> OTOH that problem is even worse than the issue I was thinking of:
>> 
>> is this valid?
>> 
> Yes.
> 
>> does it generate a different heap frame for each time through the
>> loop?
>> 
> All that happens is the stack frame is put on the heap. It doesn't
> allocate new heap frames in a loop.
> 

Well in that cases why would it be expected that the const value be frozen? 
Each time through the loop the value is stored on that stack in the same 
place. At then end of the loop, all the delegates point print out a value 
from the same location.

>> 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?
>> 
>
> Yes.
> 

Couldn't i and j overlap?

>> void main()
>> {
>> Runner a;
>> {
>> int i = 5;
>> a = {i++;}
>> }
>> {
>> int j = 6;
>> a();
>> assert(j==6);
>> }
>> }





More information about the Digitalmars-d mailing list