[Vala] for loop and closure (bug or big gotcha?)



Given this:

for (int i=0; i < 10; i++) {
    button[i].clicked.connect ((s) => {stdout.printf ("%d\n", i);});
}

all the buttons, when clicked, outputs 10.
i.e., the variable i is treated as non-local and shared
by all those closures.

Is this the correct semantic?

If this is up for debate I would say that this is
a wrong semantic. Different iteration of the for loop
should use "different" i.

hand
Nor Jaidi Tuah




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