[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Odd behaviour with multiple return values and varargs
- From: Richard Warburton <richard@...>
- Date: Fri, 14 Sep 2007 10:09:54 +1200
Tom Miles wrote:
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
and simple.
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 was negative.
local calcX
calcX = function(b)
if b<0 then return calcX(-b),"Warning Negative Fixed" end
else return math.sqrt(b) end
end
for x=-10,10 do
plot(calcX(x),10)
end
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.
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....
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
it appropriately.
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 problematic.
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)
Therefore:
print(fn(),fn()) wouldn't follow the above convention of one value per
parameter.