lua-users home
lua-l archive

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


2016-01-28 15:24 GMT+02:00 Viacheslav Usov <via.usov@gmail.com>:

> I may be missing something obvious, but there does not seem to be any
> consistent documentation in the current manual on the return values of
> metamethods.

> For example, nothing is said about the return value of the index
> metamethod when it is a function. In particular, what should happen if
> the function returns more than one value?

Any function supplied as a metamethod must have the same argument
and return value lis as the built-in function it replaces. The manual says
of __index: "If it is a function, it is called with table and key as arguments."
Since the indexing operation returns one value, so should the metamethod.

It is well documented what happens when the argument and return value
counts in a function call of a function differ from what is expected. In this
case, therefore, only the first return value is kept.

Another way of thinking about it is that a function supplied as __index
must be a drop-in replacement of rawget.

> I just had to debug some weird behaviour which was caused by such
> a metamethod, so I would be tempted to say it is wrong to return anything
> but one value, but that is not supported by official docs.

Wrong only in the sense that the extra return values are quietly
discarded. Official docs tend not say "Please don't eat the daisies".