I just want to share my vision and ask Exasol experts to aprove it or comment it.
Variable scope in the nested functions, classes, objects, etc are hard point in any programming language especially in a scripting language.
The behavior could be significantly diffirent even it looks the same in both.
The questions about iterators in DEV course. We have simple examples and real function in Query Wrapper lib.
In Lua docs here are definitions and examples could help a lot to understand how it works lua.org/pil/6.1.html
My first look on the example in DEV course was misleaded. I see in the wrapper code that function each time execute a query.
I see it could not be effective.
But it will not happens with anonymouse closure function call in return clause. Function adds only position variable.
Thus we have in memry one time executed query results and pointer ("pos").
The "Closure" function in the exaple has "i" variable and it is initialized once on the first call.
The "magic" is that we don't do it again when we call an anonymouse function in the Retrun.
Next time called function iterates because "i" variable current value is stored "somewhere" in a memory.
And only anonymouse function will be executed each time we call main function (first-class function).
So its why it is good way to program own iterator. We can store in memory only additional variables.
Thanks for comments and/or answers.
But my question when and/or how free memory? Do we need to control it or Exasol's or the wrapper lib controls it?
Exasol Wrapper git Query Wrapper ithub.com/exasol/etl-utils
From lua.org/pil/6.1.html is some explanation how "upvalue" named variable stored.
"Now, the anonymous function uses an upvalue, i, to keep its counter. However, by the time we call the anonymous function, i is already out of scope, because the function that created that variable (newCounter) has returned. Nevertheless, Lua handles that situation correctly, using the concept of closure. Simply put, a closure is a function plus all it needs to access its upvalues correctly. If we call newCounter again, it will create a new local variable i, so we will get a new closure, acting over that new variable:"
It means that the function called
a few points on your topic: