[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Upcoming changes in Lua 5.4
- From: Sean Conner <sean@...>
- Date: Fri, 19 Jan 2018 22:58:32 -0500
It was thus said that the Great Roberto Ierusalimschy once stated:
>
> > Also noticed that var args will now be put into a table - wanted to
> > check if this is just an implementation detail - i.e. no user visible
> > impact on either Lua or the C API?
>
> As already explained, you can use _ARG; but you can use also any other
> name:
>
> function foo (...=a) print(a.n) end
>
> _ARG is present both for compatibility (the above syntax will not even
> compile in older versions) and for the main chunk (which does not have
> a header to name this parameter).
Why not the following?
function foo(...)
for i = 1 , #... do
print("the answer is",...[i])
end
end
foo(1,2,nil,3,4)
the answer is 1
the answer is 2
the answer is nil
the answer is 3
the answer is 4
It gets rid of the clumsy select() function, it feels a bit more natural,
and you don't have to pack the arguments into a table. I mean, this is
trivial in C:
int foo(lua_State *L)
{
int top = lua_gettop(L);
int i;
for (i = 1 ; i <= top ; i++)
{
printf("the answer is\t%s\n",luaL_tolstring(L,i,NULL));
lua_pop(L,1);
}
return 0;
}
All this does is bring that ability into Lua. And if you still want a
table, you can always do:
a = { ... }
-spc