lua-users home
lua-l archive

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

2011/5/2 Dirk Laurie <>:
> On Mon, May 02, 2011 at 11:21:05AM +0200, Xpol Wan wrote:
> I can think of only one unavoidable application for  select("#", ...),
> unfortunately one for which an example is to be found in the standard
> library (table.insert), and that is when the calls
>    myfunc(a,b)
>    myfunc(a,b,nil)
> involve different semantics for a and b.

Actually, I have a real-life use case for which I need to count the
number of received arguments.
This is for a RPC mechanism I have implemented using a binding of the
ENet network library. I use table stringification and parsing to pass
requests and results around in messages, and since I support multiple
commands par request, and multiple return values per command, I want
to be as network-efficient as possible and to avoid useless table
levels in my data hierarchy when only one value is provided, so as to
maximize throughput.

To do so, I need to be able to detect when some RPC receives or sends
back a single value. But since this goes though a module, all my
interface makes heavy use of vararg functions.

It happens that I only have to be able to make the difference between
'1' and 'more than 1'. But it also happens that I started by writing
#..., which runs without error when I pass strings or tables, but of
course gives the wrong result since I get the length of the first
string/table. So, of course I can use select('#',...) to count safely.
But I'd rather avoid using it altogether by writing #... which happens
to be faster for me as well as the VM, to parse just fine currently,
but to be an idiom that I doubt anyone has ever actually used :-).