Use static variable in function()

后端 未结 4 2026
谎友^
谎友^ 2021-01-12 14:11

I would know if we can declare a static var in a function, as we can do in JavaScript.

When I callback my function, my variable keep her last affectation.

Or

相关标签:
4条回答
  • 2021-01-12 14:34

    You can't use static in a function.

    Global variables in Dart are no code smell because they are only library global.
    Global variables in JavaScript are ugly because they can conflict with global variables from 3rd-party libraries.
    This doesn't happen in Dart.

    As you can make a library in Dart as small as you want (for example only one variable) and you have something similar to a namespace for a library when you import it like

    import 'my_globals.dart' as gl;
    

    and then use it like

    print(gl.myGlobalValue);
    

    this is no code smell.

    You could also create a class to simulate a namespace like

    class MyGlobals {
      static myVal = 12345;
    }
    

    But library global variables are preferred in Dart instead of classes which contain only static variables or functions.

    0 讨论(0)
  • 2021-01-12 14:48

    You can use only global variables.

    Also you may solve this via private variables with "mangling" names.

    void main() {
      myFunction();
      myFunction();
      myFunction();
    }
    
    int _myFunction$count = 0;
    
    void myFunction() {
      print(_myFunction$count++);
    }
    

    This does not helps a lot but you can consider that variable with name "_myFunction$count" is a local static variable "count" in function "myFunction".

    The same as this pseudo code.

    void myFunction() {
      static int count = 0;
      print(count++);
    }
    
    0 讨论(0)
  • 2021-01-12 14:50

    You can use Closures, in this example I have created a counter as follows:

    void mian(){
     var my_counter = counter(5);
     for (var i = 0; i < 10; i++) {
       print(my_counter());
     }
    }
    
    Function counter(int n) {
      var number = 0;
      var func = () {
        if (number < n) {
          number++;
          return number;
        } else {
          number = 1;
          return number;
        }
      };
      return func;
    }
    

    or also a Generator:

    Iterable<int> count(int n) sync* {
      var number = 0;
      while (number < n) {
        yield number++;
      }
    }
    
    0 讨论(0)
  • 2021-01-12 14:51

    You can use a function object to maintain state:

    library test;
    
    class Test implements Function {
      var status = 0;
      static var static_status = 10;
    
      call() {
        print('Status: $status');
        print('Static status: $static_status');
        status++;
        static_status++;
      }
    }
    
    void main() {
      var fun = new Test();
    
      fun();
      fun();
      fun();
    
      var fun2 = new Test();
    
      fun2();
      fun2();
      fun2();
    }
    

    Output:

    Status: 0
    Static status: 10
    Status: 1
    Static status: 11
    Status: 2
    Static status: 12
    Status: 0
    Static status: 13
    Status: 1
    Static status: 14
    Status: 2
    Static status: 15
    
    0 讨论(0)
提交回复
热议问题