Hi.
Some of you may have noticed that I implemented static variables with function scope a day or two ago. Example:
int inc() { static int counter; return counter++; }
The problem in Pike is that the intended lifetime of the variable isn't always obvious; consider nested classes:
class X { int inc() { static int counter; return counter++; } }
int main() { function(:int) X1 = X()->inc; function(:int) X2 = X()->inc; write("X1:%O,%O\n" "X2:%O\n", X1(), X1(), X2()); }
Should the above output X1:0,1 X2:0, or X1:0,1 X2:2? ie should the variable be stored in X or in the top-level class?
Pike also has nested functions; consider:
function(:int) X() { int inc() { static int counter; return counter++; }; return inc; }
int main() { function(:int) X1 = X(); function(:int) X2 = X(); write("X1:%O,%O\n" "X2:%O\n", X1(), X1(), X2()); }
Same question.
I believe that the most useful would be to store the variable in the closest surrounding scope (ie X1:0,1 X2:0 for both of the above), and then have some extra syntax for specifying some other scope. The question is what the syntax should be?
Any suggestions?