lua-users home
lua-l archive

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


The problem here is that Lua does not have function declarations and so
there is no way to know that your `f' "requires" two arguments.
What happens with `tonumber' is that it is a builtin function, written in C,
and so is able to know that it requires exactly one argument, and give an
error message if not.

In other words, `tonumber' is being "nice" to the user.
C functions that receive a fixed number of arguments can do this type of
checking, but Lua functions can't.
So, C library writers have the choice to be "nice" to the user.
This is one reason that there is a difference between nil and LUA_NOOBJECT.
--lhf

>From: "Luiz Carlos Silveira" <luiz@fabricadigital.com.br>
>
>Following this concept, in the code below:
>
>function f(x,y)
>end
>function g()
>	return '1','2'
>end
>f(g(), nil)
>
>'f' only receive one parameter, right?
>but shouldn?t this statement also work like the one before?
>
>	tonumber(g(), nil)
>
>in which only one parameter should be received by the tonumber function?
>
>but it actually produces an error message:
>
>error: bad argument #2 to `tonumber' (number expected, got nil)
>stack traceback:
>  function `tonumber' [C]
>  main of string "tonumber(gsub('ab', 'a', ''), nil)"