lua-users home
lua-l archive

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


I was thinking about a problem that seemed to be just a lacking
feature of Lua. If you have a function that returns a variable number
of arguments, the only way to process those arguments is to pass them
as arguments to a vararg function, or create a new table. I was
thinking of something like a list data structure that could be added
to Lua to contain these arguments and keep them on the stack. I
envision it working as such:

function foo()
  list a = somevariablesfunc()
  callnewfuncwithlist(a)
  return a
end

Perhaps my understanding of the Lua stack is limited but I would see
it working as list being a local variable that refers to the stack
location where somevariablesfunc()'s return values are placed. The
local would refer specifically to the "top" of the return values, the
number of the return values. Basically what's put on the stack is
never removed.

This leaves a couple questions that I've been thinking about:

1) Should the list be passed as a list to other functions or be
"unpacked" before calling the function.

2) What happens with lists and closures?

I think that the list should always be unpacked when being referred to
as in callnewfuncwithlist(a), i.e. the list itself is not passed, but
its contents.

If a new closure is made inside the function referencing the list,
then it should be dealt with the same as with all locals and put on
the heap.

My last comment is that because a local value will reference the list
location on the stack, it can be properly destroyed when the function
returns.


Mainly I'm wondering what you guys think. I feel that Lua has so much
potential with variable arguments being passed around, but tables or
making specific functions for processing variable arguments seems
incredibly awkward, tables being expensive if done constantly, and
functions being expensive because of multiple calls to select(). A
list on the stack seems like the most logical and efficient solution.

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant