How does _call work?

Hello,

So I was messing around with tables, _index and __newindex and came across __call. Wanted to try it but when I try this…

local Table = {
	["Print"] = function(text)
		print(text)
	end
}

setmetatable(Table, {
	__call = function(table, ...)
		local Arguments = {...}
		
		print(unpack(Arguments))
	end,
})

Table["Print"]("Hello World")

It doesn’t print any arguments. Does anyone know how I’m supposed to use it? Am I using it wrong?

1 Like

Call works like this. No need for the print function at all.

local Table = {}

setmetatable(Table, {
	__call = function(table, ...)
		local Arguments = {...}
		
		print(unpack(Arguments))
	end,
})

Table("Hello World",4,3,2)
--Hello World	4	3	2
1 Like

I’m sure you knew this and it’s not the point of your post, but just a fun thing you can do with varargs:

__call = function(table, ...)
		print(...)
	end
3 Likes