lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Feature Request: Declaring environment when _ENV is overloaded

Open BribeFromTheHive opened this issue 3 years ago • 0 comments

Hi,

Due to the error (overloaded _ENV), I am getting a lot of undefined variables in my code. However, I have everything under control, and have identified _G as a fallback function whereas new assignments are simply fed into the class of my choice.

I would like a way to denote the environment correctly. I've tried just simply "local _ENV = table", however this still yields the same error.

Suggestion: if we have a defined class, like the below: ---@class MyClass : table

Then there should be an option to identify an environment as a component of that class, such as something like: ---@environment MyClass

Here's some example code that would allow an environment to be safely overloaded within a for ... do ... end container:

    mt = {__index = _G}
    local environment = setmetatable({}, mt)
    environment.struct = environment
    mt.__newindex = function(_, key, val) rawset(environment.struct, key, val) end
     
    ---Complicated, but allows invisible encapsulation via:
    ---for _ENV in myStruct:environment() do
    ---    x = 10
    ---    y = 100 --assigns myStruct.x to 10 and myStruct.y to 100.
    ---end
    ---@param self Struct
    ---@return function
    function Struct:environment()
        local ran
        environment.struct = self
        return function()
            if ran then
                environment.struct = environment
                return
            end
            ran = true
            return environment
        end
    end

BribeFromTheHive avatar May 10 '22 15:05 BribeFromTheHive