Re: Functions Without Returns
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Functions Without Returns
- From: Matthew Paul Del Buono <delbu9c1@...>
- Date: 2008年1月25日 14:52:38 -0500 (EST)
The reason is quite simple actually. Lua normalizes returns. If a function does not return enough values to fill all of the variables, then those variables get filled with nil:
function foo()
return 1,2;
end
local a,b,c = foo();
a and b will be 1 and 2, respectively, as expected, but c will be nil. It can't be "nothing" ... it's a variable.
You're encountering something similar:
function foo()
return;
end
local bar = foo();
print(tostring(bar))
print(tostring(foo()))
The first prints nil, because bar IS nil. A variable can't be nothing. foo(), however, returns nothing. When you run tostring() on it, nothing is passed to it. It is the same as doing tostring(), which is illegal. That is why the error arises.
-- Matthew P. Del Buono a.k.a. Shirik