lua-users home
lua-l archive

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


Your example returns nil, as you expect, but not for the reason you had.
Lua and C-functions can both return 0 values.

The reason your example returns nil, is because of this line:
function type(aValue)

If you instead change it to:

do
 local _type = type;
 function type(...)
   return _type(...);
 end
end

print(type(string.byte("")));

you will get the same behaviour as the C version, even though the new one is written in Lua.


On Sun, Jan 25, 2009 at 3:38 AM, Mark Meijer <meijer78@gmail.com> wrote:
2009/1/24 Peter Cawley <lua@corsix.org>:
> On Sat, Jan 24, 2009 at 9:08 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
>> In Lua "to return zero values" is the same as to return nil.
> No, they are not the same. The number of return values from a function
> call is corrected to number of results which the caller is expecting
> either by appending nils, or truncating the last value(s). In many
> contexts, like assigning the results of a function to variables, or
> performing a table index on the result of a function, the number of
> results is set explicitly by the caller. In other contexts, like the
> end of an argument list for function calls and table constructions, no
> appending or truncating is done.
>
> Hence the visible behaviour of returning zero values is the same
> visible behaviour as returning nil, in certain contexts. In all
> contexts, the non-visible behaviour is different, and in some
> contexts, the visible behaviour is different.

I think that general observation is mostly a result of the difference
between how C functions work with Lua and how Lua functions work. For
example consider the following:

do
 local _type = type;
 function type(aValue)
   return _type(aValue);
 end
end

print(type(string.byte("")));

I'm not in a position to test it but it should print nil, instead of
an error, due to that seemingly pointless Lua wrapper around the C
function type().