lua-users home
lua-l archive

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


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". Only
with a trick (out of the PiL) one gets "nil" (another strange thing):
> a=print("Hello")
Hello
> print(type(a))
nil
> 
If one adds an element nil explicitly, that is ok and understandable: 
> list = { 1,"text",nil,math.cos(math.pi),3*4 } 
> print(list[3])
nil
> for i,v in pairs(list) do
print (i," = ", v)
end
1	 = 	1
2	 = 	text
4	 = 	-1
5	 = 	12
> 
There I can understand that nil is not shown, because nil simply is nil,
say nothing. But in the example with the function print() I wonder about
the reported type nil? For me that seems not really logic and correct.
At best the expression should be evaluated (then Lua would be perfectly
functional :-)!)

I would await an error message in adding expressions in tables, that
will not be evaluated, as one can see from the example. As one can see,
all that is not flawless. With due care I would call that a bug?I do not
know if that may become a danger under some circumstances. 

Is there any explanation for that behaviour? 

Regards BB