DolphinDB throws an exception when reassigning an undef-ed variable

38 Views Asked by At

In DolphinDB, I defined an variable, called the undef function to undefine it, and reassigned a vector to it. But my code threw an exception which I could not understand.

To simplify the problem, I will provide a minimal example that throws the exception:

a = 0
undef(`a, VAR)
a = [1]

After executing the preceding code, I got this exception:

Assignment statement failed probably due to invalid indices [a = [1]]

I would like to know what's wrong with my code and how do I write it correctly.

1

There are 1 best solutions below

0
Davis Zhou On

To answer your question, let me explain how DolphinDB executes scripts.

DolphinDB needs to scan the script twice. In the first round, it parses all lines of the script and checks variable definitions, function definitions, function arguments, etc. DolphinDB tries to find out as many as syntax errors at parsing time. In the second round, it executes script lines one by one.

Go back to your question. In the parsing round, DolphinDB finds out one variable a and puts it into stack. The function call undef is executed at the run time (second round). It removes the variable from stack so that the third line throws an exception since the variable doesn't exist at all.