Question on scope of the `local` variables

Hi, this might be a silly question but I would really want to know more about the lua implementation in our v2 lua. This question is focused on the life span of variables. So in each of the ${} block or space-lua code block there are some variables available especially those we marked local. I wonder if 1) ${} is the same as space-lua block 2) and what variables can those blocks attend and how long would the local variables inside these blocks live?

Personally I would inclined to have a largely separated scope for each of the small block with only some predefined or global variable available. But I don’t know if my intuition is correct with the current space-lua implementation?

${} expect Lua expressions so you cannot define variables there (that would be a statement) unless you put in a self executing function along the lines of ${(function() local a = 7 return a end)()}

Local variables and functions at the top level in a space-lua block are local to that block. Any variable or function not explicitly marked local is global across your space and accessible in any context including ${}

1 Like

So a util function I defined in one space-lua block marked with local would be not accessible to another space-lua block right? I think that is pretty good design. Thanks for the explaination zef!

1 Like

That’s correct.