Why doesn't print (print ()) work like print (type (2)) in Lua?
I chatted with Lua for a couple of days and I figured out some things that made me think twice. I haven't read the Lua 5.3 reference manual yet because it seems complicated , I'll check it out soon.
Well in lua 5.3, we know that print () returns nil and prints a space.
>print(print(print()))
--this prints three spaces
--but print() returns nil so print(nil) should
--print nil. But instead it is printing 3 spaces
>print(type(2))
number --this prints a number since type(2) returns a
--number , but this doesn't work with print(print())
--why?
source to share
Returning anything from a function is not the same as returning nil
. The results can be confusing because most of the time returning nothing is interpreted similarly to returning nil
, but in case print
it doesn't print nil
because nothing is returned.
You can see the difference with the following examples:
print(select('#', (function() return end)())) -- prints 0
print(select('#', (function() return nil end)())) -- prints 1
In the first case, the number of values returned is 0, but in the second case, this number is 1, so it will show nil
as you expect when printed .
we know print () returns nil back and prints a space.
This is not true in both cases: print()
does not return nil
; it returns nothing. It also doesn't print a space, but adds a new line after all of its values are printed, so you will probably see three lines printed in the first example.
source to share