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.
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.
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 isnil
.So
if x then foo() end
will callfoo
ifx
is neithernil
norfalse
. If you want to restrict it tonil
only (and you usually don't), then usex ~= nil
.