1

I am trying to create an operator '!' that returns the print function. I am getting the following error:

line 15: attempt to call a number value
stack traceback:
    t.lua:15: in main chunk
    [C]: ?

My code is below:

local opTable = {}

debug.setmetatable(0,{
  _call = function(a,op)
    return opTable[op](a)
  end
})

local function addOp(operator,f)
  opTable[operator] = f
end

addOp('!',print)

print((5)'!')

It seems this should work, because techniqe #3 on http://lua-users.org/wiki/CustomOperators uses almost exactly the same method.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 4
    The metamethod is `__call` (two underscores) not `_call` (one underscore). – Etan Reisner Sep 02 '14 at 17:47
  • Don't try this. If you want to do this, take a look at metalua: https://github.com/fab13n/metalua Basically, Metalua will modify the source, and expand `!SomeVar` (or whatever kind of shortcuts you want to create) into `print(SomeVar)`, if you tell it how. Here's the manual: https://github.com/fab13n/metalua/blob/master/doc/manual/metalua-manual.pdf – Advert Sep 02 '14 at 19:15

0 Answers0