-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Enhancement: static local variables #8299
Comments
Added Area-Language, Triaged labels. |
You can also get the same behavior with a global private value that you don't touch anywhere else. The "static local variable" is really just a way to reduce the scope of a global variable, In Dart's "library privacy" setting you pretty much have to trust the library you are in anyway. So: It's not as obvious that _funcVal is only used inside _func(), but it's just as expressive and uses the existing lazy initialization of global/static fields. If static local fields were added, they would likely just desugar to the same code as above. Removed Type-Defect label. |
This comment was originally written by beatgam...@gmail.com True, but since this is a compile-time optimization, I couldn't accidentally re-use the same global in multiple places. |
This comment was originally written by esnellman2@gmail.com Encapsulation is a core OO principal. Yet the dart style guide: Suggests one move every static field/method into the library. Yet the stated goal of the projected was structured web apps. Does every function in Dart have a link to the parent environment in dartvm and/or dart2js? Does the compiler elevate a methods environment until it's body references something in that environment in dartvm and/or dart2js? Likewise does it inline local variables(treat them like a static const) if they are never written to? If no answers any of the cases above and these features are not planned or offered as a deployment compiler flag. Then Dart should offer syntactical sugar that lets the developer flag variables/functions as static which is translated at compile time. I want to avoid javascript closure leaks. When i am deep in some code somewhere I don't want to jump around from my current level to the class or library level to read my static helper method. By jumping around I lose structure. |
Set owner to @gbracha. |
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: