databasedolphindb

Why functions in dolphindb script can't access variables in outer scope


In most programming languages I've used, such as C/C++, python, functions could access variables in the outer scope, but as I've tried in DolphinDB script, it's not the same case.

The following code doesn't work and reports the error message: Variable 'foo' isn't initialized yet.

foo = 1
def func() {
    foo = foo + 1
}
func()

I would like to know why it is designed in this way.


Solution

  • DolphinDB supports functional programming including the feature of pure function. A pure function has no side effect, i.e. only function's input arguments can affect the output of the function.

    Pure function has clear input/output and thus improves the software quality. Pure function makes code readable. Given input arguments, a pure function has stable output, which makes test much easier. DolphinDB is a distributed system which is built on top of RPC. If a function depends on variables in outer scope, it can't be executed in remote node.