I was just wondering why __newindex was not syntaxed, cause here in my script where I use __newindex to locate new variables in my table, I noticed that when changing the name of the __newindex function to something that isn’t that, it doesn’t work anymore.
local Table = {};
setmetatable(Table, {
__newindex = function(_, index, value) -- Changing __newindex to "a" will break the function.
if type(value) == "table" then
print("New table added to index.");
print(tostring(index) ..", ".. unpack(value));
else
print("New value added to index.");
print(tostring(index) ..", ".. tostring(value));
end;
end;
});
I could be wrong, but the reason it’s not syntaxed is likely because of the name. It creates a new index inside of your metatable, changing it’s name would remove it’s purpose.
I agree it would be cool if it was syntaxed, you could call it in other ways regardless of it’s name. I think this should go in some kind of code suggestions topic on this forum.
I don’t quiet understand what you mean by that. __newindex is a metamethod and for it to be triggered, it needs to have that exact name. That’s how Lua works.
What do you mean by “syntaxed”? It doesn’t work when you change the name because __newindex is a very specific metamethod key in the metatable. Similar to __index or __tostring.
It would be nice if there were alternative ways of writing code. Wouldn’t you agree? Like if you wanted to get LocalPlayer, you could as well type :GetPlayer() to get their player. Just easier ways to code, that’s what op means. He wants more alternatives to doing the same thing.
That doesn’t make it a good idea. Why is it a good idea to let people write their metamethods ambiguously? I really don’t think that’s what the discussion seems to be about because I have no clue why anyone would want it. And I still don’t know what he means by syntaxed, or what he even expects to fix.