Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why type(nil)==nil is false?

Tags:

null

lua

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".

like image 967
corund Avatar asked Apr 27 '15 08:04

corund


People also ask

Is nil false in Lua?

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.

Why does Lua use nil?

Lua uses nil as a kind of non-value, to represent the absence of a useful value.


1 Answers

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.

like image 159
Yu Hao Avatar answered Oct 28 '22 06:10

Yu Hao