Question

Say I have the following code:

local t = {};
setmetatable(t, {__call=print});
t(3, 5, 7)

Instead of printing:

3    5    7

it prints:

table: 0x9357020    3   5   7

The id of the table is that of t.

How can I make it behave as though I called print directly?

Was it helpful?

Solution

You can't; the function specified by __call always gets passed the item that was called.

What you could do, though, is create a wrapper function that just discards the first argument and calls the function you originally wanted to call with just the arguments after the first, and set that wrapper function as the __call value.

OTHER TIPS

You cant, but you can use this code:

local t = {};
setmetatable(t, {__call=function(t,...)print(...)});
t(3, 5, 7)

Prints 3 ,5, 7 `

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top