lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> According to lua manual for lua 5.1.X, the handler looks like:
> -- Try to get a handler from the operand
> local h = metatable(op).__unm
> if h then
>   -- call the handler with the operand
>   return h(op)
> else  -- no handler available: default behavior
>   error(···)
> end
> However when I run this test program: 
> function printargs(...) table.foreach({...},print) end
> clss={}
> mt={__unm=printargs}
> setmetatable(clss,mt)
> a=-clss
> It reports that there are in fact two arguments (the table repeated twice).
> This occurs on lua 5.1, 5.1.1 & 5.1.2 compiled using mingw in windows.
> Is the manual wrong, or is there a bug in the code?

I would say the manual is wrong.

Since Lua 2.1, when fallbacks were introduced, and all the way to Lua 5.1
(going through tag methods and metamethods), the handler for unary
minus has got the same number of arguments as the other arithmetic
operations. However, until Lua 5.0, this extra argument was 'nil',
and it was documented in the manuals.

In Lua 5.1 we had to change that, because now 'nil' may have
metamethods. If the documentation (and the implementation) assumed 'nil'
as a second argument, the interpreter could end up using its metamethod
for an unrelated operation. The simplest way to correct that was to
use the original argument twice (so there are no extra metamethods
involved in the operation). We considered this second argument as "an
implementation detail" (because most functions in Lua simply ignore
extra arguments), so we did not put it in the documentation.

-- Roberto