Home > Mobile >  Does dart support non-local variables?
Does dart support non-local variables?

Time:09-27

For example in Python we have a non-local feature:

The nonlocal keyword is used to work with variables inside nested functions, where the variable should not belong to the inner function. Use the keyword nonlocal to declare that the variable is not local.

The nonlocal statement declares that whenever we change the binding of the name var, the binding is changed in the first frame in which the var is already bound. Recall that without the nonlocal statement, an assignment statement would always bind a name in the first frame of the current environment. The nonlocal statement indicates that the name appears somewhere in the environment other than the first (local) frame or the last (global) frame.

Is there something similar to this in Dart?

Here is a code sample from Python:

def make_withdraw(balance):
    """Return a withdraw function that draws down balance with each call."""
    def withdraw(amount):
        nonlocal balance                 # Declare the name "balance" nonlocal
        if amount > balance:
            return 'Insufficient funds'
        balance = balance - amount       # Re-bind the existing balance name
        return balance
    return withdraw

Pseudo-translation to Dart where I can't use nonlocal:

makeWithdraw(balance) {
  //Return a withdraw function that draws down balance with each call.
  withdraw(amount) {
    var nonlocal balance; //Declare the name "balance" nonlocal
    if (amount > balance){return 'Insufficient funds';}
    balance = balance - amount    //rebind the existing balance name
    return balance;}
  return withdraw;
}

When I type nonlocal here it's giving me error.

For context, this is where I am learning from and trying to convert the Python code to Dart:

https://composingprograms.com/pages/24-mutable-data.html#local-state

CodePudding user response:

Dart is a lexically-scoped language with explicit variable declarations. As with other programming languages that descended from C syntax, variables are scoped where they are declared. (Python needs global and nonlocal keywords because Python does not require explicit variable declarations, and without those keywords would implicitly declare new local variables.)

If you want a non-local variable, just declare it outside of the local scope. For example:

int globalVariable = 0;

void foo(int variableLocalToFoo) {
  int anotherVariableLocalToFoo = 42;

  void bar(int variableLocalToBar) {
    int anotherVariableLocalToBar = variableLocalToFoo   variableLocalToBar;
  }

  if (true) {
    int variableLocalToBlock = 9;
  }
}

class SomeClass {
  int memberVariable = 0;
}

CodePudding user response:

makeWithdraw(balance) {
  //Return a withdraw function that draws down balance with each call.
  withdraw(amount) {
    balance; 
    if (amount > balance) {
      return 'Insufficient funds';
    }
    balance = balance - amount;
    print(balance);
    return balance;
  }

  return withdraw;
}

nonlocalTest() {
  var wd = makeWithdraw(20);
  wd(5);
  wd(1);
  wd(5);

}

Ok it seems this program works fine in dart even without nonlocal variable! It seems This pattern of non-local assignment is a general feature of programming languages with higher-order functions and lexical scope..! So nonlocal is not needed in dart, but the same program will not work in python without nonlocal keyword!

  • Related