July 15, 2008 Re: D2 full closure? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | 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. > > 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. > > void main() > { > Runner a; > { > int i = 5; > a = {i++;} > } > > { > int j = 6; > a(); > assert(j==6); > } > } > > | |||
July 16, 2008 Re: D2 full closure? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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); >> } >> } | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply