Jiří Zárevúcky píše v Pá 05. 02. 2010 v 16:08 +0100: > Fredderic Unpenstein píše v Pá 05. 02. 2010 v 23:57 +1100: > > 2010/1/23 Sam Liddicott <s...@liddicott.com>: > > > I used a language called charamel for controlling 3d figurs. It's > > > closures supported > > > variables in 2 ways. > > > > > > Like valàa reference to the instantaneous value of the variable, and > > > fixing it's value > > > (making a copy) at the time the closure was made. > > > > How WOULD you do that in present Vala...? That seems like an awfully > > obvious thing you might want to do, to be so difficult. > > > > The only way to do it that I can think of, in the example above, would > > be attach the current value of i to the button as user data. Seems > > awfully convoluted, and not always particularly practical. > > > > It is awfully simple. You don't need Vala to do it. > > void main () { > for (int i = 0; i < 10; ++i) { > int t = i; > Idle.add (() => { print ("%d", t); return false; }); > } > > new MainLoop ().run (); > } > > TADAAAAA! You have the value in that particular iteration fixed. > Does anyone need something more sophisticated? > > So, why does this work? > i is defined before the loop and destroyed after it's exited. > i is the same variable in all iterations. > t is defined inside the iteration and destroyed when the iteration ends. > t is a different variable in every iteration. > > You don't need anything from Vala. The way it works is entirely logical. >
A little better example: void main () { MainLoop loop = new MainLoop (); for (int i = 0; i < 10; ++i) { int t = i; Idle.add (() => { print ("i: %d\n", i); return false; }); Idle.add (() => { print ("t: %d\n", t); return false; }); } Idle.add (() => { loop.quit(); return false; }); loop.run (); }
signature.asc
Description: Toto je digitálně podepsaná část zprávy
_______________________________________________ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list