2

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?

JasonFruit
  • 7,764
  • 5
  • 46
  • 61

2 Answers2

5

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.

Amber
  • 507,862
  • 82
  • 626
  • 550
3

You cant, but you can use this code:

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

Prints 3 ,5, 7 `

Alar
  • 760
  • 4
  • 11
  • 2
    You could also use `return print` to take advantage of tail recursion. – hugomg Feb 25 '13 at 13:31
  • Would I be correct in assuming this will perform better than `table.remove(arg, 1)`? It seems like it should, but I'm not familiar enough with Lua internals. – JasonFruit Feb 25 '13 at 15:34
  • @JasonFruit much so yes. consider that the arg table would have to be built, and all elements moved down. – daurnimator Feb 26 '13 at 11:16