I'm reading "Programming in Lua" book and I don't understand exercise 2.1:
What is the value of the expression
type(nil)==nil?
(You can use Lua to check your answer.) Can you explain this result?"
When I execute this code I get "false" as result. I could not explain this result, from my point of view correct result should be "true". I tried
type(some_undeclared_variable)==nil
and it also gives me "false".
The boolean type has two values, false and true, which represent the traditional boolean values. However, they do not hold a monopoly of condition values: In Lua, any value may represent a condition. Conditionals (such as the ones in control structures) consider false and nil as false and anything else as true.
Lua uses nil as a kind of non-value, to represent the absence of a useful value.
The function type() always returns a string, the value of type(nil) is the string "nil", which is not the same as nil, they have different type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With