VSCode-EmmyLua icon indicating copy to clipboard operation
VSCode-EmmyLua copied to clipboard

Scoping bug when combining local with function assignment

Open Vladinator opened this issue 4 years ago • 2 comments

image

I suspect it is because I define a function using a local variable that should be then considered local function but it ends up placed in the global scope.

Vladinator avatar Aug 17 '20 10:08 Vladinator

Very interesting. Test4 was defined as local, then it temporary becomes global, because you define a new global function with the same name and it overwrite local variable, so at the end it is local again, but emmylua "remember" that it was global.

peek

Technically you're right, it's a bug (maybe), but you use a very weird way to define this function. You should use

local function Test4() end

instead of

local Test4
function Test4() end

Even if it's really a bug, I like it, because it helps to don't write shitty code :D

winterwolf avatar Sep 14 '20 23:09 winterwolf

Thanks for taking time to look into this ticket.

The origin for this was purely accidental. Sometimes I use(d) this technique to scope variables out of the rest of the code like:

local MyFunc do
    local internalReusableVariables
    local internalFuncs() end
    function MyFunc(i) end
end

Purely to have MyFunc exposes as a local to the file scope but have the other internal variables and functions in their own scope to limit autocomplete madness if the file got some size and a lot of these were supposed to not be used elsewhere outside that particular function anyway.

Vladinator avatar Sep 15 '20 07:09 Vladinator