Full closures for D
Xinok
xnknet at gmail.com
Fri Nov 2 15:54:10 PDT 2007
It seems that variables that are used by a nested function are allocated
on the heap rather than the stack. This was my test code:
void delegate() foo(){
int v = 60;
int c = 35;
writefln(&c);
writefln(&v);
return {writefln(&v);};
}
void main(){
void delegate() one = foo();
one();
}
Prints:
12FF18
8B2FF4
8B2FF4
The address of 'v' doesn't change. What you do notice is the great
difference of the memory addresses between int v and int c, which
suggests that 'v' is allocated on the heap rather than the stack.
Nathan Reed wrote:
> Walter Bright wrote:
>> D 2.007 brings full closures to D. I was tired of D being denigrated
>> for not having "real" closures.
>
> Very cool. I have to admit I didn't think this would make it into the
> language. :)
>
> I'm curious about how they work. I'd guess you dup the stack frame onto
> the heap when a function returns a delegate, and update the delegate's
> context pointer?
>
> Thanks,
> Nathan Reed
More information about the Digitalmars-d
mailing list