Reputation: 21
I 'm not sure is it ok to use if x then foo() end
to detect x is not nil in Lua.
There's so many detect like this in the project at hand, so I need a suggestion should I change them all or let them go.
Thanks.
Upvotes: 1
Views: 58
Reputation: 473272
An expression is considered false, for the purposes of any condition statement, on exactly two conditions: the expression is the boolean value false
or the expression is nil
.
So if x then foo() end
will call foo
if x
is neither nil
nor false
. If you want to restrict it to nil
only (and you usually don't), then use x ~= nil
.
Upvotes: 2