lua-users home
lua-l archive

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


> On Jun 12, 2015, at 1:53 PM, Patrick Donnelly <batrick@batbytes.com> wrote:
> 
> On Fri, Jun 12, 2015 at 3:14 PM, Tim Hill <drtimhill@gmail.com> wrote:
>> What is the reason luaL_callmeta() only allows one return value? I’m guessing it’s because it’s primarily targetted for metamethods like __add(). However, it would be great if there was a luaL_callmetax() that had an explicit argument for the number of return values (or LUA_MULTRET).
>> 
>> Not academic: All my projects to date have needed this (of course I’ve created my own, but it’s surprising that it’s not there for things like __pairs() etc).
> 
> With the exception of the special-case __call, all metamethod
> operations implicitly truncate results to 1 value in the language, I
> don't think a special case for C makes sense. How surprising would it
> be if
> 
> a(b+1)
> 
> passed more than one argument to a?
> 
> Fortunately for __call, you can easily use lua_call instead.
> 
> -- 
> Patrick Donnelly
> 

Well yes but I see no sign of Lua internally using luaL_callmeta() for this purpose; the “low-level” metamethods (__add etc) use a different code path. luaL_callmeta() seems only to be used for the “high-level” metamethods like __tostring(), and of course it’s NOT used for __pairs() since this metamethod returns 3 values (this is where I encountered this issue). So the argument about controlling the number of return values for the low-level metamethods doesn't apply here.

I would have expected luaL_callmeta() to be a macro wrapper around a more generic luaL_callmetax(), much like many of the other Lua APIs. Especially as this is supposed to be a convenience high-level API.

—Tim