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



2010/1/23 Sam Liddicott <sam 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.


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

What about an optional second list of closure-local variables, either
with assigned values, or taking their value from the immediate value
of the variable of the same name, as in:

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

Here, the i seen within the closure, isn't the same i seen by the for
loop, and holds the value of the outer i as it was at that particular
invocation of the statement.  Likewise the j inside the closure
doesn't exist at all in the outer function, but is another
closure-local variable initialised to a value, again, evaluated at
that particular invocation of the statement.


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

Would be a definite bonus.  I'm pretty sure I'll slip up there sooner
or later myself, and spend a bit of time scratching my head.


Fredderic



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