lua-users home
lua-l archive

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


Luiz Henrique de Figueiredo wrote:
> 
> >For a long time I advocate to make C-functions behave similar to
> >Lua functions regarding argument handling (make missing args nil).
> 
> This is easy to do: If you expect k arguments on the stack, do lua_settop(k)
> as the first thing in your C function. Missing arguments will become nil and
> extra arguments will be "deleted".

Yeah, but first you have to remove all upvalues, for vararg functions you
have to check top first because you don't want to remove additional arguments,
none of the luaL_optXXX functions will work any more, all libraries or even
functions within a library have different argument handling, ...

> The current design, in which C functions have freedom to decide what to do
> when the number of arguments passed is not what is expected, seems to us
> to be more flexible and the right one for writing libraries, even if it
> sometimes may be confusing.

Just give 0 as the "minimum number of arguments" value in my proposed change
and you get all freedom back.  But IMHO it would be better to enforce a little
bit of standard function behaviour in C functions.  C functions should behave
similar to regular Lua functions and should have consistent argument handling.
Just look at the problems people had to just write a wrapper for tinsert.
IMHO the C calling conventions are too loose and produce unnecessary
complications.

> As we say in our meetings: C is not Lua! :-)
> This is serious: in C you can do anything, and so there's no hand-holding
> and you're also expected to work harder.

It's not about working harder.  It's about consistent function argument
handling - in the regular libraries, in extension libraries and Lua code.

And making it a little bit easier for the C programmers is not a bad thing
either.

Ciao, ET.