Enhancement: static local variables #8299
Labels
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
closed-not-planned
Closed as we don't intend to take action on the reported issue
type-enhancement
A request for a change that isn't a bug
This issue was originally filed by beatgam...@gmail.com
In C/C++, we can do something like this:
int func() {
static int val = 0;
return val++;
}
printf("%d\n", func()); // outputs 0
printf("%d\n", func()); // outputs 1
printf("%d\n", func()); // outputs 2
In C++, for classes we can do the same:
class Thing {
public:
int func() {
static int i = 0;
return i++;
}
};
int main() {
Thing* a = new Thing();
Thing* b = new Thing();
printf("%d\n", a->func()); // outputs 0
printf("%d\n", a->func()); // outputs 1
printf("%d\n", a->func()); // outputs 2
printf("%d\n", b->func()); // outputs 3
printf("%d\n", b->func()); // outputs 4
printf("%d\n", b->func()); // outputs 5
}
For translating this to JS, we can simulate this with:
function func() {
if (!func.val) func.val = 0;
return func.val++;
}
The only functional difference seems to be that func.val is visible outside func. This can also be done with prototypes in the same way.
I propose the syntax be just like the C/C++ syntax in Dart:
func() {
static int val = 0; // or static var val = 0;
return val++;
}
The best arguments against this that I could come up with are:
* doesn't exist in Java (weak)
The text was updated successfully, but these errors were encountered: