lua-users home
lua-l archive

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



On 13 Sep 2007, at 23:33, Adrien de Croy wrote:


I wonder if there's some reason no other language (that I know of anyway) allows more than one return value for a function.

Common Lisp?

Maybe because it's not such a great idea to have a non- deterministic number of return values?

Also, surely if Lua is going to validate the number of input parameters to a function, it should validate the number of return values?

Lua doesn't validate the number of input parameters to a function. In fact Lua, _unlike_ most other languages, acts very symmetrically with regard to calling and returning. After all, returning is just calling the continuation, so the two cases _should_ be treated the same.

In Lua any number of arguments can be passed to any function regardless of how many it expects. Excess arguments are discarded, a shortfall is made up with nils. There is a mechanism for arbitrary numbers of arguments.

Any number of results can be returned from a function, regardless of how many are required. Excess results are discarded, a shortfall is made up with nils. There is a mechanism for arbitrary numbers of results.

I'm not dead against more than one return value, but I think it's a terrible idea to have a variable number of return values (esp dependent on input). How on earth can a caller know how many return values there actually were?

If a function wants to return lots of values, it should return a table (which at least then can be iterated). At least then the stack doesn't grow (or does lua clean up unused return values?).

e.g

for i in 1, 1000000 do
   c = funcreturning2values()
end

unless lua pops unused return values off the stack, you'll have a stack size of 10000000 items by the end of this.

This is pure speculation. You haven't looked at the implementation. It doesn't do that. That would be silly.

drj