lua-users home
lua-l archive

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


On 2 April 2011 13:18, C++ RTMP Server <crtmpserver@gmail.com> wrote:
> Hello,
>

> Suppose I have to provide lua with a C/C++ impl of a ListFolder function. What would be the "desired" return form? Table? Or multiple values return?
> Both of them work just fine, but I'm interested in what would be the most intuitive way for lua programmers?
>

If you mean listing the contents of a folder or something - there are
actually 3 options:

1) Multiple return values

I think multiple return values are most useful in the case where the
number of values is (roughly) known. If it's an arbitrary number of
return results, you basically have to put them in a table to do
anything useful.

2) Provide an iterator

This is what LuaFileSystem does (lfs.dir). It's a flexible approach,
and allows you to simply run a loop over the results, which is 90% of
the time what you want. It also means you don't have to create a table
to hold all the results (in theory there could be many) if you don't
need to.

3) Return a table

Returning a table is handy, it allows you to index the results
(result[1], results[2], etc.) or unpack() them (to get them as
multiple return results, as in #1) and it allows you to easily iterate
over them with ipairs() (as in #2). The only downside is that it's an
allocation overhead, but 99% of the time this won't matter so the
other benefits outweigh it.

Hope this helps,
Matthew