[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Odd behaviour with multiple return values and varargs
- From: Adrien de Croy <adrien@...>
- Date: Fri, 14 Sep 2007 10:33:35 +1200
I wonder if there's some reason no other language (that I know of
anyway) allows more than one return value for a function.
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?
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
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?).
for i in 1, 1000000 do
c = funcreturning2values()
unless lua pops unused return values off the stack, you'll have a stack
size of 10000000 items by the end of this.
Richard Warburton wrote:
Tom Miles wrote:
I was thinking that calcX may be part of a library that I didn't
write. I could check for an error message, but if I'm aware of the
issue and don't care....
I'm not sure I understand your point. In my mind, if I have two
functions returning 2 values, then I have 4 values plain
It would be treated exactly the same as print(1,2,3,4). Is there a
reason not to do it that way?
Hang on, unlike other languages a function can return different
number of arguments depending on input. Now print isn't picky about
arguments as it is just outputting them to std out. However, if you
had a function that required x and y for some parabolic eqn, things
could get messy if x relied on a fn that returned two values when x
calcX = function(b)
if b<0 then return calcX(-b),"Warning Negative Fixed" end
else return math.sqrt(b) end
for x=-10,10 do
You don't want returns mucking up input arguments
I guess people are using multiple return values in entirely different
ways. Personally, if I have a I know how many values it returns and use
all the values. In the above example, what is the point of returning the
error string, as you'll never see it.
Good point, but Lua was designed AFAIK to be easy for anyone to pick
up and trying to track down a bug due to a function you didn't write
spitting out more vars than expected due to rare input might be
You're correct in saying you
don't want returns mucking up input arguments, but surely you should
know what is being returned from a function in all cases and deal with
Many lua functions return one value if successful and two on failure.
I'm really beginning to think that I must be confined to a single way of
thinking, and am missing something in the bigger picture.
No it's just different. It could also be due to:
x,y =1,2 (x=1,y=2)
x = 1,2 (2 discarded, x=1)
x,y,z = 1,2,3,4 (4 discarded)
print(fn(),fn()) wouldn't follow the above convention of one value per
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com