Skip to content
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

Closed
DartBot opened this issue Feb 4, 2013 · 5 comments
Closed

Enhancement: static local variables #8299

DartBot opened this issue Feb 4, 2013 · 5 comments
Assignees
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

Comments

@DartBot
Copy link

DartBot commented Feb 4, 2013

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)

@kasperl
Copy link

kasperl commented Feb 5, 2013

Added Area-Language, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Feb 7, 2013

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:
  int _funcVal = 0;
  int func() => _funcVal++;
and:
  class Thing {
    static int _funcVal = 0;
    int func() => _funcVal++;
  }

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.
Added Type-Enhancement label.

@DartBot
Copy link
Author

DartBot commented Feb 16, 2013

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.

@DartBot
Copy link
Author

DartBot commented Oct 9, 2013

This comment was originally written by esnellman2@gmail.com


Encapsulation is a core OO principal. Yet the dart style guide:
https://www.dartlang.org/articles/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?
example: var t = (int i) => i+2;

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.

@gbracha
Copy link
Contributor

gbracha commented Jan 3, 2015

Set owner to @gbracha.
Added NotPlanned label.

@DartBot DartBot added Type-Enhancement 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 labels Jan 3, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
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
Projects
None yet
Development

No branches or pull requests

5 participants