lua-users home
lua-l archive

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


It has nothing to do with tostring specifically, it could happen with
any function. In Lua, you need a vararg function fct(...) to
dstinguish between fct() and fct(nil), but in the C API it is much
more obrusive. You can test for nil, or for nil-or-none, and in the
case of tostring, the authors feel that none does not has a string
equivalent, so it is an error.

Op Di. 5 Mrt. 2019 om 07:21 het Magicks M <m4gicks@gmail.com> geskryf:
>
> function f() end
> tostring(f()) --what happens here?
> This code causes an error, which I think is not obvious, with the message "bad argument #1 to 'tostring' (value expected)" I believe this is because the number of values returned by f is 0 which means the vararg passed into tostring has length 0.
>
> This is quite odd in terms of how the code looks, now consider the following function:
> function transform(f)
> return function(x, ...) return f(x, ...) end
> end
> This looks rather redundant, but transform(tostring)(f()) will not produce an error, which is perhaps even weirder.
>
> Is this supposed to behave like this?