GNOME Bugzilla – Bug 660927
lambda does not work correct with foreach statement
Last modified: 2011-10-04 20:33:10 UTC
====== test.vala ======= delegate void P (); void main(string[] argv) { int[] numbers = {1,2,3}; foreach (int i in numbers) { P p = () => {stdout.printf("%d %d\n", i, i * i);}; p(); } } The i in lambda function is always zero. It should be 1, 2, 3.
Thanks for the bug report. This particular bug has already been reported into our bug tracking system, but please feel free to report any further bugs you find. Copy "i" into a local variable within the foreach block as a workaround. *** This bug has been marked as a duplicate of bug 628336 ***