lua-users home
lua-l archive

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



On Sat, Jun 20, 2009 at 10:21 PM, bb <bblochl@arcor.de> wrote:
If one makes a table with an _expression_ as an element, one will get the
following:

> list = { 1,"text",print("Hallo"),math.cos(math.pi),3*4 }
Hallo
> print(#list)
5
> for i,v in pairs(list) do
print (i," = ", v)
end
1        =      1
2        =      text
4        =      -1
5        =      12
> print(list[3])
nil
>

The strange thing is, that there the _expression_ print("Hallo") is dealt
as "nil"? "nil" certainly is a valid data type in Lua. If I crosscheck
that result, a problem arises:
> print(type(print("Hello"))
>>
As one might see, the _expression_ "print("Hello")" is not "nil".

You've got mismatched parentheses here, three opening and only two closing. The ">>" is Lua's interpreter telling you that it expects more input.

If you do add that third closing parenthesis, you will get an error, because a call to print(...) has no return values. Therefore type(print("Hello")) is effectively the same as type(). Remember that in Lua functions can return an arbitrary number of values, and this number can be zero. When you do something like "a = func()" and func returns no values, Lua will assign nil instead.

-Duncan