Re: [Vala] Confusing closure behavior




Digging a little deeper, I have some more confusion.  This makes the
for-loop and the foreach-loop behave differently.  What do you think of:

// capturing the for variable should capture a copy if the variable is
// local to the for loop

public delegate void Thunk();

int main() {
        Thunk thunk = () => {};
        for (int i = 0; i < 1; i++) {
                thunk = () => {
                        stderr.printf ("%d = 0, right?\n", i);
                };
        }
        thunk ();
        int[] indices = { 0 };
        foreach (var i in indices) {
                thunk = () => {
                        stderr.printf ("%d = 0, right?\n", i);
                };
        }
        thunk ();
        return 0;
}




Confusingly,

Vivien


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]