www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Lazy and delegates - out-of-scopeness - what happens?

reply Cecil ward <janet janetward.com> writes:
If I pass lazy expressions or delegates to as arguments to functions, what
happens with variables mentioned in the
lazy expression or in the delegate body if those variables are on the stack or
otherwise out of scope, or are objects
that may be freed? Does the compiler partially protect the programmer from
mistakes here?

If anyone can comment on how these features are implemented in generated code
wrt stack-allocated objects that
are _in_ lexical scope in a delegate evaluation context, then I'd be interested
to hear about this.
Jul 14 2011
parent Jacob Carlborg <doob me.com> writes:
On 2011-07-14 20:42, Cecil ward wrote:
 If I pass lazy expressions or delegates to as arguments to functions, what
happens with variables mentioned in the
 lazy expression or in the delegate body if those variables are on the stack or
otherwise out of scope, or are objects
 that may be freed? Does the compiler partially protect the programmer from
mistakes here?

 If anyone can comment on how these features are implemented in generated code
wrt stack-allocated objects that
 are _in_ lexical scope in a delegate evaluation context, then I'd be
interested to hear about this.
They all local variables (or similar) that a delegate references will be allocated on the heap, this is also known as a closure: http://en.wikipedia.org/wiki/Closure_%28computer_science%29 -- /Jacob Carlborg
Jul 14 2011