Problem with closures
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Thu Jan 11 20:16:02 PST 2007
Lutger wrote:
> Alain wrote:
>> Frits van Bommel Wrote:
>>
>>> Alain wrote:
>>>> AddFunc addN(int n) {
>>>> int add(int i) {
>>>> return i + n;
>>>> }
>>>> return &add; // the add function captured will remember the
>>>> value of n
>>>> }
>>> [snip]
>>>> Am i missing something?
>>> You shouldn't return delegates to nested functions, just like you
>>> shouldn't return pointers to local variables.
>>> Delegates to nested functions contain a pointer to the stack frame of
>>> the enclosing function. If that function has returned, the stack
>>> frame may be corrupted (especially if you have called another
>>> function, like apply(), since then).
>>
>> How come this compiles? Is it a bug ?
>>
>> Alain
>
> This is valid code. It is up to the programmer to make sure the delegate
> doesn't use vars that are out of scope. There was some talk about this,
> to copy the 'evironment' for later use, it might be solved in the future.
>
> std.bind might be of use here.
As could a template.
<code>
import std .stdio ;
alias int delegate(int n) AddFunc;
template TAddN (int N) {
const TAddN = (int i) { return i + N; } ;
}
void apply (int[] array, AddFunc func) {
foreach (inout i; array) {
i = func(i);
}
}
void main () {
int[] numbers = [1,2,3] ;
writefln("numbers before = ", numbers);
apply(numbers, TAddN!(40));
writefln("numbers after = ", numbers);
}
</code>
-- Chris Nicholson-Sauls
More information about the Digitalmars-d-learn
mailing list