[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Lua 5.1 varargs with nil question
- From: nr@... (Norman Ramsey)
- Date: Sun, 23 Apr 2006 23:14:59 -0400 (EDT)
I have some code that uses Lua 5.0 and has varargs functions
in which some arguments may be nil. Here is an example using Lua 5.0:
local output
--- function to output zero or more items, where an item is
--- * a string
--- * nil (ignored)
--- * a function returning zero or more items
--- * a list of items in a Lua table
do
local output_funs = { }
output_funs['string'] = output_one
output_funs['nil'] = function() end
output_funs['table'] = function(t) return output(unpack(t)) end
output_funs['function'] = function(f) return output(f()) end
function output(...)
for i = 1, table.getn(arg) do --- can't use ipairs b/c arg might be nil
local v = arg[i]
assert(output_funs[type(v)], "Bad value to output()") (v)
end
end
end
I am struggling to figure out how to implement this function in Lua 5.1.
How am I to make sure that I get all the arguments when the new #
operator is not specified when some arguments are nil?
Must I write C code to capture the number of arguments?
int argc(lua_State *L) {
lua_pushnumber(L, lua_gettop(L));
return 1;
}
Depressing if true...
Norman