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



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. 

Perhaps some [ attributes ] to the closure could spcify which variables are to be frozen static, or perhaps 
atributes appled at varable use to signify use of the static value

In any case vala can recognize that a closure is using a loop variable to issue a warning.

Sam

-----Original Message-----
From: JürgBilleter <j bitron ch>
Sent: 20 January 2010 08:28
To: Nor Jaidi Tuah <norjaidi tuah ubd edu bn>
Cc: vala-list gnome org
Subject: Re: [Vala] for loop and closure (bug or big gotcha?)

On Wed, 2010-01-20 at 14:00 +0800, Nor Jaidi Tuah wrote:
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?

Yes.

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.

The scope of the variable `i` covers the whole `for` statement, not just
a single loop iteration (otherwise i++ at the end of each iteration
wouldn't make any sense). While it's true that this may cause mistakes,
we can't change this without breaking consistency. foreach loops should
be more convenient in that regard.

Jürg

_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list




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