It's not possible to implement select's length function in pure Lua. Let me show you an attempt so I can point out the flaw.
I should clarify, it's not possible in CC's LuaJ, at least. The following works in ordinary C-based Lua, but not on CC.
function length(...)
return #({...})
end
Issue here, is that the table constructed by {…} will cut off at the first nil. So length("a", nil, "b") will return 1.
A recursive attempt yields the same result.
function length(first, ...)
if first == nil then
return 0
else
return length(...) + 1
end
end
Here, we again cut off at the first nil. You could argue that you can just check the second parameter, but what if the call was length(nil, nil, nil)? The result should be three but we get 0.
So in LuaJ, the only way to get the expected result of length(nil, nil, nil) is this
function length(...)
return select("#", ...)
end
Because select isn't implemented in Lua. It's in Java, so it has facilities to actually check the length of the arguments array.