lua-users home
lua-l archive

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


On Apr 2, 2011, at 3:55 PM, Matthew Wild wrote:

> 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.
I agree. But my question was only an example. I know the ListFolder function is actually functionality duplicated, but I just gave it for the sake of example. The more generic question would be "Should we return multiple values (of the same type) as an array or as a MVR?"

> 
> 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,
It does (in my particular case, I should return a table because I don't know the number of results)! But instead of flooding the mailing list with newbie questions (or wasting a lot of time searching through it) I was hoping for something like a link to a "best practices" kind of thingy. 

> Matthew
> 

Thank you very much for the response
------
Eugen-Andrei Gavriloaie
Web: http://www.rtmpd.com