lua-users home
lua-l archive

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


On 2017-07-13 18:32, Viacheslav Usov wrote:
On Thu, Jul 13, 2017 at 6:12 PM, Roberto Ierusalimschy wrote:
I hope you do not expect that the previous sentence ("Parameters
act as local variables that are initialized with the argument
values") applies also to C functions.

[…] we somehow must expect that setmetatable should behave like a "C
function" and not a like a Lua function. […]

Similarly for the next sentence ("A vararg function [...] collects
all extra arguments and supplies them to the function through a
vararg expression").  So, I see no reason to expect that this
particular sentence would apply to C functions.

In fact, the behaviour of C functions can be most easily understood
by assuming they are always vararg. Even then the TNONE/TNIL
difference remains, but at least the "adjustment" is taken out.

Almost… ^,^  The easiest way to understand C functions from Lua is that
they are always _vararg-only_ functions with no named parameters.  TNONE
doesn't exist on the Lua side (it's an artifact of indexing beyond top
of stack) but can be emulated by checking if the index is bigger than
`select('#',...)`.

So you can actually treat C functions as a feature-limited subset of Lua
functions, which is easy to understand.

-- nobody