Problems with functions scopes


#1

Why do a code like this one :

var a = 0
def printA()
{
    print(a.to_string())
}
printA()

Doesn’t work ?
It says :

Can not find object: a


#2

Functions cannot access variables outside of their scope. If you want a global variable you can do:

GLOBAL a = 0
def printA() {
  print(a)
}

printA()

Or you can make a lambda and bind it:

var printA = fun[a]() {
  print(a);
}

Or you can take a parameter and bind something like that:

var a = 0

def printA(aparam) { print(aparam); }
var printABound = fun[a]() { printA(a); }

#3

Okay :slight_smile: So that wasn’t a bug, I was trying to do that because it’s possible in many languages like C++ or Python.
Are you gonna implement that or not ?
If not I’ll go with the methods you gave me, thanks :slight_smile:


#4

No, it won’t be implemented, it would be a big change from existing behavior.


#5

Okay, thanks for the answers :slight_smile: