Why is __newindex not syntax highlighted?

Hello,

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.

1 Like

Yeah I noticed changing the name from __newindex to something else breaks it, and that’s because __newindex is a metamethod.

For some reason, metamethods aren’t syntaxed & I’m unsure as to why.

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.

1 Like

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.

It might be due to code like this being valid:

local t = setmetatable({}, {
	aProperty = "hi!",
	aMethod = function(self, ...)
		print(...)
	end,
})

print(getmetatable(t).aProperty)
print(getmetatable(t):aMethod("hello", "world"))

You can basically put anything, but I don’t see why people would do that

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.

He’s aware of that already as we discussed that. I think what he means is why not allow it to be syntaxed so it’s name can be changed alternatively.

I still don’t understand what it means for it to be syntaxed. Syntax is just the rules of a language.

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.

1 Like

I think he means syntax highlighting, and that is simply because it is not a built-in global nor a keyword.

2 Likes

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.

Oops, sorry. I do mean syntax highlighting. Let me change the topic-name.

I meant syntax highlighting. It’s not highlighted for some reason. None of the metamethods are.