What does rawset and rawequal do?

When reading the metatable devhub post I saw rawset and rawequal and I dont understand the description of it well.

Can anyone give me a simplified, understandable description?

1 Like

rawset sets a value of a table without invoking the __newindex metamethod.

For example, the code below would cause a stack overflow because when a value is set with self[i] = v, the __newindex metamethod would be invoked again and the function would just keep recursively calling itself.

local t = setmetatable({}, {
	__newindex = function(self, i, v)
		self[i] = v
	end
})

t[1] = "Hello world!"
> C stack overflow

To prevent this you could use rawset so the __newindex metamethod doesn’t get invoked when you set a value

local t = setmetatable({}, {
	__newindex = function(self, i, v)
		rawset(self, i, v)
	end
})

t[1] = "Hello world!"
print(t[1])
> Hello world!

rawequal is the same but for the __eq metamethod.

5 Likes