lua-users home
lua-l archive

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

Peter Wang wrote:
>     if n then
>         return gsub(s, pat, repl, n)
>     else
>         return gsub(s, pat, repl)
>     end
> end
> This might have been asked before, but why didn't you guys make
> `gsub' also accept `nil' for the final parameter?

It's because of how optional arguments in C functions are handled.
They actually check the _number_ of arguments and not whether an
argument is nil (as it is done in Lua itself).

I quote myself: [2 April 2000]
>   In my opinion there's an unnecessary difference between handling
>   of arguments to C functions and lua functions.  A lua function
>   f(a,b) cannot say if it was called via f(a,nil) or f(a).  A C
>   function gets either a nil or LUA_NOOBJECT for b.  Why this
>   difference?  Isn't nil supposed to stand for "no object"?  ;-)
>   I think it would have been better to return nil for not passed
>   arguments.  The functions that check for optional arguments
>   check against nil instead of LUA_NOOBJECT (then giving exactly the
>   same behavior as lua functions).  And an additional function (i.e
>   lua_narg()) gives the real number of passed arguments to simulate
>   vararg lua functions.

Sol handles arguments of C functions as proposed.  The switch to 4.0
would have been the perfect time to change this API in Lua ...

Ciao, ET.

PS: My old server is now longer alive.  The new location of Sol is: