Add warning when setting dictionary key with same variable name

When setting a key in a dictionary, it will always make the key a string instead of using the variable with the same name. This can be confusing. Adding a warning when setting the key on line 2 would make this clearer

local Test = 123
local Table = {Test = true}

print(Table.Test) --123
print(Table[Test]) --nil
8 Likes

This would add a lot of noise to my class instantiation code, which often looks like

function MyClass.create(param1, param2)
    local c = {
        param1 = param1,
        param2 = param2,
        -- other members ...
    }
    setmetatable(c, MyClass)
    return c
end
1 Like

I often use same names for variables and dictionary entries for internal values in some of my instances, so I am against this request. If you really have this issue, then consider renaming your variables to something that wouldn’t conflict with indices.

3 Likes

This is not workable for my own codebase, I’d get a lot of faux warnings. No thanks.

7 Likes